Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5936955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:28:16+00:00 2026-05-22T15:28:16+00:00

Apologies in advance for the length of this question! I have a data structure

  • 0

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?

  • 1 1 Answer
  • 4 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:28:17+00:00Added an answer on May 22, 2026 at 3:28 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apologies in advance if this question is badly worded! I have a MySQL table
Apologies in advance if this is a silly question but I'm wondering which might
Apologies in advance, because I suspect this may be a silly question. I have
Apologies in advance if this has been addressed before, but I've tried looking through
All, Apologies in advance - this question might be too open-ended for SO. Anyway...
This may be a bit of an abstract question, so apologies in advance. I
This, I'm sure is a pretty basic question about JavaScript, so apologies in advance.
Apologies in advance if this is a silly question, am very much a beginner.
Apologies in advance but I have never seen this error before and don't know
I am newbie to objective-c.So apologies in advance if this question sounds simple.As we

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.