질문 : 고객에게 주문을 삽입하는 LINQ와 엔터티 코드는 무엇입니까?LINQ-to-Entities를 사용하여 특정 테이블에 데이터를 삽입하는 방법은 무엇입니까?
using (OrderDatabase ctx = new OrderDatabase())
{
// Populate the individual tables.
// Comment in this next line to create a new customer/order combination.
// Customer customer = new Customer() { FirstName = "Bobby", LastName = "Davro" };
// Comment in this line to add an order to an existing customer.
var customer = ctx.Customers.Where(c => c.FirstName == "Bobby").FirstOrDefault();
Order order = new Order() { OrderQuantity = "2", OrderDescription = "Widgets" };
// Insert the individual tables correctly into the hierarchy.
customer.Orders.Add(order);
// Add the complete object into the entity.
ctx.Customers.AddObject(customer);
// Insert into the database.
ctx.SaveChanges();
}
물론 ADO.NET을 사용하여 작업을 수행 할 수는 있지만 기본/외래 키를 처리하여 내 모델을 "오염"시키고 싶지는 않습니다. 위의 코드에는 기본/외래 키가 없습니다. LINQ to Entities에서이 키를 처리합니다. – Contango