0

I am trying to devise a good way to perform updates to a SQL Server database using WCF Data Services and Entity Framework. The problem I'm having is that it seems overly complex to perform update, delete, and insert operations using the service.

I'll use typical Customer / Invoices scenario to help explain my current approach. I'm using WPF MVVM for the application. My view model contains a customer object that receives updates from the user. When saving, I pass the customer object to the service. The service must then load the customer object, transfer the property values from the updated customer, then perform the save.

Something like this:

public static int SaveProgram(Customer entity)

int returnValue = 0;

// Setup the service Uri
    Uri serviceUri = new Uri(Properties.Settings.Default.DataUri);

try
    {
    // Get the DB context
            var context = new DevEntities(serviceUri);

            Customer dbCustomer;
            if (entity.CustomerId == 0)
            {
                dbCustomer = new Customer();
                context.AddToCustomers(dbCustomer);
            }
            else
            {
                dbCustomer = context.Customers.Where(p => p.CustomerId == entity.CustomerId).FirstOrDefault();
            }
    if (dbCustomer != null)
    {
                dbCustomer.StatusId = entity.StatusId;
                dbCustomer.FirstName = entity.FirstName;
                dbCustomer.LastName = entity.LastName;
                dbCustomer.Address = entity.Address;
        ...
    }

    context.UpdateObject(dbCustomer);

    // Submit Changes
    DataServiceResponse response = context.SaveChanges(SaveChangesOptions.Batch);
    // Check for errors
    ...

    returnValue = response.Count();
}
... Catch exceptions

return returnValue;

}

Is it really necessary to go through all of this? It seems there should be an easier way.

Adding an invoice requires something like this:

var newInvoice = Invoice.CreateInvoice(0, customerId, etc...);
context.AddRelatedObject(dbCustomer, "Invoices", newInvoice);

Having already added a new invoice to the Customer.Invoices collection, this seems cumbersome.

Deleting an invoice is even worse. To delete an invoice I have to compare the invoices collection from the database with that of the passed in entity. If I cannot find a database version of the invoice in the entity.Invoices collection, then I know it should be deleted.

I have the feeling that I must not be approaching this correctly.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Mark Bonafe
  • 1,461
  • 13
  • 23
  • 1
    Seems like an EF-only question: How do you save a disconnected object? You don't need to clone it to another object, either call AddOjbect if it's new or Attach and mark it modified as shown [here](http://stackoverflow.com/questions/4255581/save-disconnected-object-in-entity-framework-4) – Panagiotis Kanavos May 16 '14 at 16:08
  • I've been working down that path, Panagiotis. I am using EF 6.1, so it appears the syntax has changed. I have tried context.AttachTo("Customers", entity) followed by context.UpdateObject(dbcustomer). Unfortunately, calling save returns an error that is of no use to me at all; an error occurred while processing this request. I have set UseVerboseErrors = true on the service and still get this message. Very frustrated right now. – Mark Bonafe May 16 '14 at 17:58

0 Answers0