Question: what is the LINQ-to-Entity code to insert an order for a specific customer?
L2E does not support set-based operations currently (update without select). See Use linq to generate direct update without select
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).
Notice that Order has a Customer property. You don't have to add the Order to the Customer -- you can do it the other way around. So, instead of creating a new Customer, get the Customer using Linq, then add it to your new Order.
using (OrderDatabase ctx = new OrderDatabase())
{
ctx.AddOrder(new Order()
{
OrderQuantity = 2,
OrderDescription = "Widgets",
Customer = ctx.Customers.First<Customer>(c => c.CustomerId == yourId)
});
ctx.SaveChanges();
}
I don't get what the problem is, exactly.
var mycustomer = context.Customers.Where(x => x.id == 100).FirstOrDefault();
if(mycustomer != null)
{
mycustomer.Orders.Add(myorder);
}
context.SaveChanges();