Hi I am using an observable collection which I am displaying in the datagrid on the wpf screen.
I have linked this with ADO.NET Entity Data Model object. When I make any change to the data in the datagrid and do SaveChanges() this gets saved in the database. But if I add a row then that doesnt get added. Where am I going wrong.
The code used: Customer is a ADO.NET Entity Data Model object added for the table CustomerTable in SQL database.
Customer contextCust = new Customer();
var list = from e in contextCust.CustomerTable select e;
custCollection = new ObservableCollection<CustomerTable>(list);
Now when I change any data in the existing datagrid on the UI display I call the function:
contextCust.SaveChanges();
This works fine. But if I add a row on the UI, it gets added to the observable collection but how do I push this to the contextCust. Looks like only data changes are allowed by SaveChanges method but not adding of rows.
Is there a way I can cast observable collection to CustomerTable type and make contextCust consume the same. Kindly advise.
Thanks, T
Well if you add another row into your grid (assuming you are creating a Customer
) you have to add it to contextCust.CustomerTable
using the DbSet.Add Method as well to track it for SaveChanges
Things added to your ObservableCollection
will not be added to this DbSet
as they are not connected/synced magically.