// POST api/products
 public virtual HttpResponseMessage Post(Invoice invoice)
 {
     if (invoice == null)
     {
         return Request.CreateResponse(HttpStatusCode.BadRequest);
     }
     else
     {
         using (AriUMContext ctx = new AriUMContext("AriUMDBConnection"))
         {
             if (invoice.Customer != null)
             {
                 //
                 Customer customer = (from c in ctx.Customers
                                      where c.CustomerId == invoice.Customer.CustomerId
                                      select c).FirstOrDefault<Customer>();
                 if (customer != null)
                 {
                     invoice.Customer = customer;
                 }
                 else
                 {
                 }
             }
             // Calculate invoice number
             int maxInvoiceNumber = (from inv in ctx.Invoices
                                     where inv.Year == invoice.Year
                                     select inv.InvoiceNumber).Max();
             invoice.InvoiceNumber = maxInvoiceNumber + 1;
             Invoice i = CntWebApiVerbs.PostInvoice(invoice, ctx);
             FetchStrategy fs = new FetchStrategy();
             fs.LoadWith<Invoice>(x => x.Customer);
             Invoice idt = ctx.CreateDetachedCopy<Invoice>(i,fs);
             var response = Request.CreateResponse<Invoice>(HttpStatusCode.Created, idt);
             response.Headers.Location = GetInvoiceLocation(idt.InvoiceId);
             return response;
         }
     }
 }
 // PUT api/products/5
 public virtual HttpResponseMessage Put(int id, Invoice invoice)
 {
     if (invoice == null || id != invoice.InvoiceId)
     {
         return Request.CreateResponse(HttpStatusCode.BadRequest);
     }
     else
     {
         using (AriUMContext ctx = new AriUMContext("AriUMDBConnection"))
         {
             // Does it exist?
             Invoice i = CntWebApiVerbs.GetInvoice(id, ctx);
             if (i == null)
             {
                 return Request.CreateResponse(HttpStatusCode.NotFound);
             }
             else
             {
                 i = CntWebApiVerbs.PutInvoice(invoice, ctx);
                 FetchStrategy fs = new FetchStrategy();
                 fs.LoadWith<Invoice>(x => x.Customer);
                 Invoice idt = ctx.CreateDetachedCopy<Invoice>(i, fs);
                 return Request.CreateResponse<Invoice>(HttpStatusCode.OK, idt);
             }
         }
     }
 }
 public static bool DeleteInvoice(Invoice invoice, AriUMContext ctx)
 {
     ctx.Delete(invoice);
     ctx.SaveChanges();
     return true;
 }
 public static Invoice PutInvoice(Invoice invoice, AriUMContext ctx)
 {
     //Customer c = invoice.Customer;
     //invoice.Customer = null;
     //ctx.AttachCopy<Invoice>(invoice);
     //invoice.Customer = c;
     int customerId = 0;
     int invoiceId = invoice.InvoiceId;
     if (invoice.Customer != null)
     {
         customerId = invoice.Customer.CustomerId;
         invoice.Customer = null;
     }
     ctx.AttachCopy <Invoice>(invoice);
     if (customerId != 0)
     {
         invoice = (from i in ctx.Invoices
                    where i.InvoiceId == invoiceId
                    select i).FirstOrDefault<Invoice>();
         invoice.Customer = (from cus in ctx.Customers
                             where cus.CustomerId == customerId
                             select cus).FirstOrDefault<Customer>();
     }
     ctx.SaveChanges();
     return invoice;
 }
 public static Invoice PostInvoice(Invoice invoice, AriUMContext ctx)
 {
     ctx.Add(invoice);
     ctx.SaveChanges();
     return invoice;
 }