best practise/way for master detail / multi table Insert in Entity Framework

前端 未结 2 723
被撕碎了的回忆
被撕碎了的回忆 2020-12-16 23:49

My table structure is this

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

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

         


        
2条回答
  •  不思量自难忘°
    2020-12-17 00:22

    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.

提交回复
热议问题