My table structure is
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 perform an insert operation using Entity Framework using the code below
Is this the best way to make an insert?
I am not happy with the way I get the complete product element from the context object, instead of just assigning a simple value to productId
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);
Is this the right way to insert the main part or is there a better / different way.
Binj antony
source share