views:

840

answers:

1

I have a simple Entity Framework. For simplicity's sake I will transform the entity names to Northwind Like.

I have a IEnumerable Of Customer and Item Objects

I am trying to Create Orders which of course is made up (simplistically) of an Item and A Customer.
How can I go about creating and inserting these Orders?

-Hcane

A: 

Try this

Using context As New MyEfContext
  Dim order As New Order
  order.Customer = context.Customers.FirstOrDefault(Function(cust) cust.Id = custId)
  Dim item As Item
  item = context.Items.FirstOrDefault(Function(item) item.Id = itemId)
  If (Not(item Is Nothing)) Then _
    order.Items.Add(item)
  context.AddToOrders(order)
  context.SaveChanges()
End Using
bendewey
Wrap it into a Using block.
Inferis
Apart from the mixing of VB and C# that worked perfectly ,...the key was the context.AddToOrders not Order.Insert
Hurricanepkt
Yea my VB.Net is a bit rusty, I spend most of my day in C#.
bendewey