Before storing modifications, EntityFramework displays entities.

c# entity-framework savechanges

Question

With its function ToList, the Entity Framework ObjectSet only displays stored entities. Consequently, when I call

context.AddToCustomers(myNewCust);

after that (without calling SaveChanges)

myDataGrid.DataContext = context.Customers.ToList();

The newly created object is not shown in the DataGrid (despitecontext.Customers.Count() does not include it).

Is it possible to display these entities (those havingEntityState = Added ) ?

I appreciate it.

1
9
3/28/2011 7:51:48 AM

Accepted Answer

I believe you may get newly added entities that have not been stored by dialing:

var inserted = context.ObjectStateManager
                      .GetObjectStateEntries(EntityState.Added)
                      .Where(e => !e.IsRelationship)
                      .Select(e => e.Entity)
                      .OfType<Cutomer>();

However, based just on reading your inquiry, I'm afraid that you are attempting a mistake. Why is it necessary to merge retrieved and unsaved entities? Simply store it in your own distinct collection if you need to display unsaved stuff.

9
3/28/2011 8:23:48 AM

Popular Answer

Please have a look at the TryGetObjectStateEntry(EntityKey, ObjectStateEntry) function.

http://msdn.microsoft.com/en-us/library/system.data.objects.objectstatemanager.aspx



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow