4

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?

Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654
Shevek
  • 3,449
  • 4
  • 37
  • 60
  • i found this resource helpful setting up everything correctly with [STE's](http://skysigal.xact-solutions.com/Blog/tabid/427/entryid/2344/Default.aspx). – Menahem May 19 '11 at 14:05
  • assuming you are using WCF to communicate with the client, did you reuse the generated STE classes when generating the WCF proxy/reference ? if not , the tracking wont take place but no error will be thrown. – Menahem May 19 '11 at 14:08
  • At the moment I am calling from a Test project using a Visual Studio generated Private Accessor – Shevek May 19 '11 at 14:16
  • by privtae accessor , do you mean the client is calling the server through WCF, or directly using a local variable ? – Menahem May 19 '11 at 14:19
  • not through WCF - using Microsoft.VisualStudio.QualityTools.UnitTestFramework Private Accessor – Shevek May 19 '11 at 14:29
  • A lot of code and a lot of text but I absolutely don't understand your examples. For example what is relation between `customer`, `savedCustomer`, `newCustomer` instances? – Ladislav Mrnka May 19 '11 at 21:29

1 Answers1

2

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.

Shiraz Bhaiji
  • 60,773
  • 31
  • 133
  • 239
  • This only works if the Save and Update are performed under the same context. – Shevek May 23 '11 at 09:40
  • The save and update happen in the DAL and each one has it's own context. How should I handle this? Should the context be created in the BLL and passed to the DAL so it can be used by both Save and Update? – Shevek May 23 '11 at 09:42
  • I went with creating the context in the BLL and passing to the Create and Update methods in the DAL - this works! Many thanks – Shevek May 23 '11 at 10:39