Search code examples
.netentity-frameworkentity-framework-4entity-relationshipself-tracking-entities

How to stop Self-tracking entity performing Add instead of Update


Apologies in advance for the length of this question!

I have a data structure from which the following Enity Data Model has been created (tables/fields renamed and simplified for ease of understanding!):

Entity Model

The PaymentMethod / ProductPaymentMethod structure exists because a Customer can have multiple PaymentMethods available but may choose from them which to use per Product.

The CustomerReference, ProductReference and VendorReference are all generated based on the CustomerId so must be updated after the initial Save.

The ProductPaymentMethods need the PaymentMethodIds so must be added after the initial Save.

The ADO.NET Self-Tracking Entity Generator has been run to generate the self-tracking context and object classes.

I have a CreateCustomer method in the BLL which generates a new Entity as follows (psuedo-code):

Customer newCustomer = new Customer()
Product newProduct = new Product()
Vendor newVendor = new Vendor()
List<Currency> newCurrencies = new List<Currency> { new Currency(), new Currency() }
List<Frequency> newFrequencies = new List<Frequency> { new Frequency(), new Frequency() }
List<PaymentMethod> newPaymentMethods = new List<PaymentMethod> { new PaymentMethod(), new PaymentMethod() }
newProduct.Vendors.Add(newVendor)
newProduct.Currencies.Add(newCurrencies)
newProduct.Frequencies.Add(newFrequencies)
newCustomer.Products.Add(newProduct)
newCustomer.PaymentMethods.Add(newPaymentMethods)

newCustomer is then passed to CreateCustomer method in the DAL which does this:

context.Customers.AddObject(customer)
context.SaveChanges()
return customer

The return is assigned to a new Customer object in the BLL and the references generated from the CustomerId and added:

savedCustomer.CustomerReference = generatedCustomerReference
savedCustomer.Products.First().ProductReference = generatedProductReference
savedCustomer.Products.First().Vendors.First().VendorReference = generatedVendorReference

The savedCustomer.PaymentMethod collection is looped through to create List<ProductPaymentMethod> productPaymentMethods using the PaymentMethodIds. This is then added:

savedCustomer.ProductPaymentMethods.Add(productPaymentMethods)

newCustomer is then passed to UpdateCustomer method in the DAL which does this:

context.Customers.ApplyChanges(customer)
context.SaveChanges()
return customer

This is then returned to the Presentation layer

This resulted in duplicates of everything that had been created and then updated - I'd end up with 2 x Customer, 2 x Product, 2 x Vendor, 4 x PaymentMethod, 4 x Currency and 4 x Frequency records - UpdateCustomer was receiving an entity marked as Added

So after a bit of research I added the following before calling UpdateCustomer:

savedCustomer.MarkAsModified()
savedCustomer.Products.First().MarkAsModified()
savedCustomer.Products.First().Vendors.First().MarkAsModified()

This stopped the duplicated Customer, Product and Vendor but I was still getting duplicated Currency, Frequency and PaymentMethod records.

The solution I came up with was to loop through each of the collections and mark each of the entites as unchanged before calling UpdateCustomer:

foreach (currency in Customer.Products.First().Currencies)
    currency.MarkAsUnchanged()

(repeated for Customer.PaymentMethods and Customer.Products.First().Frequencies)

I now get no duplication and all my data is created.

Finally, on to my question!

I can't help but feel that I am missing something here. Do I really have to check each part of the relationship tree and mark as Modified or UnChanged (nice mixture of terms there Microsoft!) as necessary before each and every update?

I thought the term 'Self-Tracking' meant that all that should be handled automatically.

Is this the correct way to use EF / STE or is there a better way?

EDIT: A little more info regarding my Visual Studio Solution.

DAL project - CustomerModel.edmx, CustomerModel.Context.tt and CustomerDAL.cs

Model project - CustomerModel.tt

BLL project - CustomerBLL.cs

WCF project - CustomerWCF.svc.cs

Test project CustomerTest.cs

CustomerTest.cs uses Private Accessor to call CustomerWCF.svc.cs

CustomerWCF.svc.cs calls CustomerBLL.cs

CustomerBLL.cs calls CustomerDAL.cs

DAL references Model

BLL references DAL and Model

Service references BLL and Model

Test references Service, BLL and Model

Should I be testing against a ServiceReference instead of the PrivateAccessor?


Solution

  • EF will perform a Add if it thinks it is a new object and update if it thinks that it is an existing.

    If you create a object in code, with new ... then EF assumes that it is a new object.

    If you get an object from the database, change the values and then do a save, EF knows that it is an existing object and will do an update.

    So if you have an object that has been created in code, but may exist in the database, you must first retrieve it from the database, update it and then do a save.