How do I use LINQ-to-Entities to insert data into a specific table?

前端 未结 4 1598
自闭症患者
自闭症患者 2020-12-11 21:28

Question: what is the LINQ-to-Entity code to insert an order for a specific customer?

\"enter

4条回答
  •  天命终不由人
    2020-12-11 21:43

    Your code isn't far off. Just change your second line to read as follows:

    Customer customer = ctx.Customer.FirstOrDefault(c => c.FirstName == "Bobby");
    if (customer != null)
    {
        //...
    

    Just replace the c.FirstName == "Bobby" with something that can strongly identify the customer you're looking for (e.g. c.Id == customerID if you already know what the ID is).

提交回复
热议问题