views:

1145

answers:

2

My table structure is this

Orders
------ 
Id int identity
OrderDate smalldatetime
OrderStatusid tinyint

Products
--------
Id int identity
Name varchar(50)

OrderDetails
------------
Id int identity
OrderId int (fkey)
ProductId int (fkey)
Amount decimal
Rate decimal

I am trying to an insert operation using Entity Framework using the code below
Is this the best way to do the insert?
I am not happy with the way I am getting the full product item from the context object, instead of being able to just assign a simple productId value

using (MyContextEntities ctx = new MyContextEntities())
{
    Orders newOrder = new Orders()
    {
    Name = "Gayle Wynand",
    OrderDate = DateTime.Now,
    IsComplete = true,
    Comments = "test",
    OrderStatusId = 2,
    IsActive = true
    };
    OrderDetails ode = new OrderDetails();
    ode.Products = ctx.Products.First(p => p.Id == 2); // any other way?
    ode.Quantity = 2;
    ode.Rate = 5.2;
    newOrder.OrderDetails.Add(ode);

    OrderDetails ode2 = new OrderDetails();
    ode2.Products = ctx.Products.First(p => p.Id == 3); // any other way?
    ode2.Quantity = 3;
    ode2.Rate =6.5;
    newOrder.OrderDetails.Add(ode2);


    ctx.AddToOrders(newOrder);
    ctx.SaveChanges();
}

Is this the correct way to do the master detail insert or is there a better/another way.

+2  A: 

What you are doing now will work just fine.

If you would like to avoid doing a database query when assigning ode.Products, then you could use the following alternative:

// substitute your actual qualified entity set name
ode.ProductsReference.EntityKey = 
    new EntityKey("MyEntities.ProductsEntitySetName", "Id", 2);

This is faster, but less readable. Also, the Products property will be null until you Load it. But for an insert, this is often OK.

Craig Stuntz
And is there any shortcuts to update master detail data?
Binoj Antony
I'm not sure what you mean. What kind of shortcuts are you expecting? Can you be more specific about your needs?
Craig Stuntz
I wanted to know how I would do the exact same action done above but for an update operation.
Binoj Antony
Because it looks like I need to again do the query from the db to get the entities then change the property (to change the enityState) then do the update(SaveChanges), any simpler/easier way?
Binoj Antony
That is right. For an update you must first select, then save.
Craig Stuntz
How about calling AcceptChanges then just changing one property then calling SaveChanges()? This way we can avoid the select..
Binoj Antony
Might work. Try it!
Craig Stuntz
A: 

Another approach would be to use Stub Objects rather than EntityKeys i.e.

var product = new Product {ID = 2};
ctx.AttachTo("Products", product);
ode.Product = product;

etc. As an added bonus this code will work with POCO objects too in the future.

See this blog post for more information on that technique.

Alex James
I tried this first and found that when calling ctx.SaveChanges() it tries to insert even the Products
Binoj Antony