开发者

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(); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜