I have Data = new ObservableCollection<DATA>(dataContext.DATA);
dataContext is my Entities. DATA is SQL Server table.
Now I run Import method, which fills DATA table on SQL Server:
public static void Import(DataTable table)
{
var dataContext = DataContext.Instance.Entities;
foreach (DataRow row in table.Rows)
{
.......
var dataRow = new DATA
{
.........
};
dataContext.AddToDATA(dataRow);
}
dataContext.SaveChanges();
}
But my ObservableCollection Data is still empty. How do I update it?
ObservableCollection<T> Constructor (IEnumerable<T>):The keyword here is that it copies the element from the collection. After that it does not retain any synchronization with the enumeration that you passed in. Your code is updating the
dataContextobject, not yourDataobject (The ObservableCollection). You will either have to build out your own synchronized version of ObservableCollection or create methods to keep them in sync.If you aren’t concerned with keeping the
dataContextobject synchronized and only wanted to use it in the one-time load, then you have to have all the appropriate objects in that enumeration before using it in the constructor of your ObservableCollection.My guess is you do something similar to the following:
It should be: