EF SaveChanges() - id vs loaded object to ForeignKey
I found strange thing. For example I have Order and Item entity. Item is reference on Order by id. In EF to save Item, I need firstly load Order, and put it to reference field? Is this only way?
Order order = data.OrderSet.FirstOrDefault(b => b.OrderID == OrderId);
Item item = new Item { order = order };
data.AddToAbzacSet(item);
开发者_开发百科 data.SaveChanges();
No, it is not. With EF 1.0 you can do it this way using reference object:
Item item = new Item();
item.OrderReference.EntityKey = new EntityKey("YourContext.Order", "ID", OrderID);
data.AddToAbzacSet(item);
data.SaveChanges();
精彩评论