Hi
When I try to update table using following code I get error message
"Cannot create an instance of the variable type 'T' because it does not have the new() constraint"
How Can I create a new instance and update data in database.
IEnumerable<BookData> bookData = GetBookData(...);
BookDataContext bookDB = new BookDataContext ();
bookDB.UpdateTable<MCDB.BookTable>(bookDB.BookTable,bookData);
class BookTable
{
public string bookName { get; set; }
public string authorName { get; set; }
}
Extension Method
public static void UpdateTable<T>(this DataContext context, Table<T> tbl, IEnumerable<BookData> data)
where T : class
{
foreach (var item in data)
{
context.GetTable<T>().InsertOnSubmit(new T());
// Error in above statement
// I want to do something like this
context.GetTable<T>().InsertOnSubmit(new T
{
bName = item.bookName
aName = item.authorName,
});
}
context.SubmitChanges();
}
PS: I know the another way but I want to update table using extension method
using ( BookDataContext bookDB = new BookDataContext ())
{
//code
}
Thanks