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 159761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:54:57+00:00 2026-05-11T10:54:57+00:00

I am trying to save my contact, which has references to ContactRelation (just the

  • 0

I am trying to save my contact, which has references to ContactRelation (just the relationship of the contact, married, single, etc) and Country. But everytime I try to save my contact, which is validated I get the exception ‘ADO.Net Entity Framework An entity object cannot be referenced by multiple instances of IEntityChangeTracker’

public Contact CreateContact(Contact contact) {     _entities.AddToContact(contact); //throws the exception     _entities.SaveChanges();     return contact ; } 

I’m using a loosely coupled MVC design with Services and Repositories. I’ve read a lot of posts about this exception but none give me a working answer…

Thank you, Peter

  • 1 1 Answer
  • 0 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. 2026-05-11T10:54:58+00:00Added an answer on May 11, 2026 at 10:54 am

    [Update]
    Because L2E is used you need to save all the linked objects first before you can save the main object. Which makes sense otherwise you would create (in my example) an artist without it’s contact object. This isn’t allowed by the database design.
    [/Update]

    Here’s my implementation which worked.

    [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create([Bind(Exclude = 'Id')] Artist artist, [Bind(Prefix = 'Contact')] Contact contact, [Bind(Prefix = 'Country')] Country country, [Bind(Prefix = 'ContactRelationship')] ContactRelationship contactRelationship) {     ViewData['Countries'] = new SelectList(new CountryService(_msw).ListCountries().OrderBy(c => c.Name), 'ID', 'Name');     ViewData['ContactRelationships'] = new SelectList(new ContactRelationshipService(_msw).ListContactRelationships().OrderBy(c => c.ID), 'ID', 'Description');      country = _countryService.GetCountryById(country.ID);     contact.Country = country;     contactRelationship = _contactRelationshipService.GetContactRelationship(contactRelationship.ID);     contact.ContactRelationship = contactRelationship;     if(_contactService.CreateContact(contact)){         artist.Contact = contact;         if (_service.CreateArtist(artist))             return RedirectToAction('Index');             }     return View('Create'); } 

    And then in my ContactRepository :

    public Contact CreateContact(Contact contact) {     _entities.AddToContact(contact); //no longer throws the exception     _entities.SaveChanges();     return contact ; } 

    I also found on this website that it is best to keep the same context throughout the application so I’m now using a special Data class for this:

    Rick Strahl and Samuel Maecham have taught me that you should keep your datacontext per user per request. Which means putting it in the HttpContext for web applications. Read all about it here

    public class Data {     public static MyDBEntities MyDBEntities     {         get         {             if (HttpContext.Current != null && HttpContext.Current['myDBEntities'] == null)             {                 HttpContext.Current['myDBEntities'] = new MyDBEntities ();             }             return HttpContext.Current['myDBEntities'] as MyDBEntities;         }         set {              if(HttpContext.Current != null)                 HttpContext.Current['myDBEntities'] = value;          }     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You are modifying a copy of the $text you passed… May 11, 2026 at 2:01 pm
  • added an answer There are two "sys" catalog views you can consult: sys.indexes… May 11, 2026 at 2:01 pm
  • added an answer Imperative languages are typically built around the concept of state.… May 11, 2026 at 2:01 pm

Related Questions

I am trying to save data to a database on a button push, but
I am using asp.net and I am trying to save checkbox values into a
I have an asp.net app and I am trying to save a text file
I am trying to scrape an html table and save its data in a
I am trying to set a javascript date so that it can be submitted
I have written a few Python tools in the past to extract data from
I am trying to save some bandwidth and include wsdl file in my flex/air
I am trying to send my dynamically created silverlight 2 page/image to a an
I am currently trying to figure out how to save the content from a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.