views:

77

answers:

1

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();
+3  A: 

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();
Misha N.