I wanted to write this small, trivial extension method for some throwaway test / console app and I haven't been able to figure out how to get the method signature right. I'm looking to add this to every System.Data.Linq.Table that I have and I want it to take an instance of type T as a parameter.
Here's what I have so far that doesn't compile
public static void InsertAndSubmit<T>(this System.Data.Linq.Table<T> tbl, T element)
{
tbl.InsertOnSubmit(element);
tbl.Context.SubmitChanges();
}
The type 'T' must be a reference type in order to use it as parameter 'TEntity' in the generic type or method 'System.Data.Linq.Table'
Try this:
public static void InsertAndSubmit<T>(this System.Data.Linq.Table<T> tbl, T element)
where T : class
{
tbl.InsertOnSubmit(element);
tbl.Context.SubmitChanges();
}
You need to constrain the type of T
to be a reference type (class
) in order for this to work.
If you look at Table<TEntity>
's documentation you will notice this same type constraint on TEntity
of class
. So for your T
to be compatible with TEntity
it must meet the same constraints.
You need to specify that you will use only refernece types in your T
. Try:
public static void InsertAndSubmit<T>(this System.Data.Linq.Table<T> tbl, T element)
where T : class
{
tbl.InsertOnSubmit(element);
tbl.Context.SubmitChanges();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With