Domanda: qual è il codice LINQ-to-Entity per inserire un ordine per un cliente specifico?Come utilizzare LINQ-to-Entities per inserire dati in una tabella specifica?
Aggiornamento
Ecco la soluzione (si veda una delle risposte presentate qui sotto per una soluzione molto più pulito):
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();
}
Ovviamente, potrei probabilmente usare ADO.NET per fare il lavoro - ma non voglio "inquinare" il mio modello trattando chiavi primarie/esterne. Si noti che il codice sopra non ha chiavi primarie/esterne - LINQ to Entities gestisce questo per me. – Contango