Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Entity Framework ObjectSet with its method ToList shows just saved entities. That means, when I call

context.AddToCustomers(myNewCust);

and then (without calling SaveChanges)

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

the DataGrid doesn't show the newly added entity (even context.Customers.Count() doesn't include it).

Is there any way to show these entities (those with EntityState = Added) ?

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
429 views
Welcome To Ask or Share your Answers For Others

1 Answer

I think you can get unsaved added entities by calling something like:

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

But just by reading your question, I'm affraid that you are trying to do something wrong. Why do you need to combine unsaved entities with retrieved? If you need to show unsaved content you should simply keep it in your own separate collection.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...