5

My EF Core entity:

public class Order {
    public string Code { get; set; }  
    public ICollection<LineItem> LineItems { get; set; }  
}

I can get the original value for the Code property using:

context.Entry(myOrder).OriginalValues["Code"]

But if I try that for the LineItems navigation collection property, then it throws with:

The property LineItems on entity type Order is being accessed using the 'Property' method, but is defined in the model as a navigation property. Use either the 'Reference' or 'Collection' method to access navigation properties.

That exception message doesn't refer to methods I can find.

So how do I get original values for a navigation collection property?

grokky
  • 8,537
  • 20
  • 62
  • 96
  • 1
    Look at the methods of `context.Entry(myOrder)`. – Gert Arnold Dec 14 '16 at 12:18
  • 2
    Use context.Entry(myOrder).Collection("LineItems") as mentioned in exception – Ankit Rana Dec 14 '16 at 12:26
  • @grokky you want to get the current collection of order that saved in the database ? – Ankit Rana Dec 14 '16 at 12:47
  • @FooVirus Not the "current" collection, but the "original" collection. The original values before any changes. I thought the change tracker tracked collections just like it tracks non-collections. – grokky Dec 14 '16 at 13:02
  • @grokky context.Enty(myOrder).Collection(x => x.LineItems).CurrentValue : Gets current value of the navigation property Or use context.Enty(myOrder).Collection(x => x.LineItems).Load() to get original dbvalue but if entities that already exist in the context are not overwritten with values from the database. – Ankit Rana Dec 14 '16 at 13:08
  • see this http://stackoverflow.com/questions/3635071/update-relationships-when-saving-changes-of-ef4-poco-objects/3635326#3635326 ,i think this may help you – Ankit Rana Dec 14 '16 at 13:23
  • Nope it doesn't take a lambda, and besides, Load() doesn't load the old entities. It's the original/old ones I need. – grokky Dec 14 '16 at 14:21
  • Have you found a way to get the original values from the navigation property? – Alexandre Jobin Apr 18 '19 at 16:00

0 Answers0