Note that there are some explanatory texts on larger screens.

plurals
  1. POSolution to "An entity object cannot be referenced by multiple instances of IEntityChangeTracker" error
    primarykey
    data
    text
    <p>I am attempting to update a member, which has quite a few relationships, but receive an error saying that <code>An entity object cannot be referenced by multiple instances of IEntityChangeTracker</code>. Below are the various models and controllers that I have written. Any suggestions on how to correct this would be appreciate. </p> <p><strong>Member Model</strong> </p> <pre><code>public partial class Member { NCOWW_DB db = new NCOWW_DB(); public Member() { this.Contacts = new HashSet&lt;Contact&gt;(); this.Utilities = new HashSet&lt;Utility&gt;(); } public int MemberID { get; set; } [Display(Name = "Member Name")] [Required] public string Name { get; set; } [Display(Name = "Member Number")] public string Number { get; set; } [Display(Name = "Mutual Aid Agreement On File")] public bool MutualAid { get; set; } [Display(Name = "Type ID")] public Nullable&lt;int&gt; TypeID { get; set; } [ForeignKey("TypeID")] public virtual UtilityType MemberType { get; set; } [Display(Name = "Water Population")] public Nullable&lt;int&gt; WaterPopulation { get; set; } [Display(Name = "Wastewater Population")] public Nullable&lt;int&gt; WastewaterPopulation { get; set; } private Address _Address = new Address(); public Address Address { get { return _Address; } set { _Address = value; } } private Coordinates _Coordinates = new Coordinates(); public Coordinates Coordinates { get { return _Coordinates; } set { _Coordinates = value; } } [NotMapped] public double Distance { get; set; } public bool Deleted { get; set; } [Display(Name = "Enable Member")] public bool Enabled { get; set; } private DateTime _createdOn = DateTime.UtcNow; public DateTime CreatedOn { get { return _createdOn; } set { _createdOn = value; } } private DateTime _modifiedOn = DateTime.UtcNow; public DateTime ModifiedOn { get { return _modifiedOn; } set { _modifiedOn = value; } } public System.Guid ModifiedBy { get; set; } //public virtual ICollection&lt;Utility&gt; Contacts { get; set; } private ICollection&lt;Contact&gt; _Contacts; public virtual ICollection&lt;Contact&gt; Contacts { get { return (from c in db.Contact where (c.MemberID == MemberID &amp;&amp; c.Deleted == false) select c).OrderBy(c =&gt; c.FirstName).ToList(); } set { _Contacts = value; } } //public virtual ICollection&lt;Utility&gt; Utilities { get; set; } private ICollection&lt;Utility&gt; _Utilities; public virtual ICollection&lt;Utility&gt; Utilities { get { return (from u in db.Utility where (u.MemberID == MemberID &amp;&amp; u.Deleted == false) select u).OrderBy(u =&gt; u.Name).ToList(); } set { _Utilities = value; } } } </code></pre> <p><strong>Utility Model</strong></p> <pre><code> public partial class Utility { NCOWW_DB db = new NCOWW_DB(); public Utility() { this.Contacts = new HashSet&lt;Contact&gt;(); this.Resources = new HashSet&lt;UtilityResource&gt;(); } [Key] public int UtilityID { get; set; } [Display(Name="Member")] [Required] public int MemberID { get; set; } [ForeignKey("MemberID")] public virtual Member Member { get; set; } [ForeignKey("UtilityID")] public virtual ICollection&lt;UtilityResource&gt; Resources { get; set; } [ForeignKey("UtilityID")] private ICollection&lt;Contact&gt; _Contacts; public virtual ICollection&lt;Contact&gt; Contacts { get { return (from c in db.Contact where (c.MemberID == MemberID &amp;&amp; c.Deleted == false) select c).OrderBy(c =&gt; c.FirstName).ToList(); } set { _Contacts = value; } } //public virtual ICollection&lt;Contact&gt; Contacts { get; set; } [Required] public string Name { get; set; } private Address _Address = new Address(); public Address Address { get { return _Address; } set { _Address = value; } } private Coordinates _Coordinates = new Coordinates(); public Coordinates Coordinates { get { return _Coordinates; } set { _Coordinates = value; } } [Display(Name = "Utility Type")] public int? TypeID { get; set; } [ForeignKey("TypeID")] [Display(Name = "Utility Type")] public virtual UtilityType UtilityType { get; set; } [Display(Name = "Region")] public int? RegionID { get; set; } [Display(Name = "County")] public int? CountyID { get; set; } private UtilityInfo _WaterInfo = new UtilityInfo(); [Display(Name="Water Information")] public UtilityInfo WaterInfo { get { return _WaterInfo; } set { _WaterInfo = value; } } private UtilityInfo _WastewaterInfo = new UtilityInfo(); [Display(Name = "Wastewater Information")] public UtilityInfo WastewaterInfo { get { return _WastewaterInfo; } set { _WastewaterInfo = value; } } [NotMapped] public double Distance { get; set; } private bool _enabled = true; public bool Enabled { get { return _enabled; } set { _enabled = value; } } public bool Deleted { get; set; } private DateTime _createdOn = DateTime.UtcNow; public DateTime CreatedOn { get { return _createdOn; } set { _createdOn = value; } } private DateTime _modifiedOn = DateTime.UtcNow; public DateTime ModifiedOn { get { return _modifiedOn; } set { _modifiedOn = value; } } public Guid ModifiedBy { get; set; } } </code></pre> <p><strong>Contact Model</strong></p> <pre><code> public partial class Contact { public Contact() { this.Phones = new HashSet&lt;ContactPhone&gt;(); } [Key] public int ContactID { get; set; } public Nullable&lt;Guid&gt; UserID { get; set; } [Display(Name = "Member")] public int MemberID { get; set; } public Nullable&lt;int&gt; UtilityID { get; set; } public string Username { get; set; } [Display(Name = "First Name")] [Required] public string FirstName { get; set; } [Display(Name = "Last Name")] [Required] public string LastName { get; set; } [NotMapped] public string FullName { get { return FirstName + " " + LastName; } } [NotMapped] public string FullNameLastNameFirst { get { return LastName + ", " + FirstName; } } [Display(Name = "Contact Type")] public string Type { get; set; } [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")] [Required] public string Email { get; set; } [ForeignKey("ContactID")] public virtual ICollection&lt;ContactPhone&gt; Phones { get; set; } [NotMapped] [Display(Name = "Grant Website Access")] public bool WebsiteAccess { get; set; } public bool Deleted { get; set; } private DateTime _createdOn = DateTime.UtcNow; public DateTime CreatedOn { get { return _createdOn; } set { _createdOn = value; } } private DateTime _modifiedOn = DateTime.UtcNow; public DateTime ModifiedOn { get { return _modifiedOn; } set { _modifiedOn = value; } } public System.Guid ModifiedBy { get; set; } public virtual Member Member { get; set; } public virtual Utility Utility { get; set; } [NotMapped] public string Password { get; set; } [NotMapped] [Compare("Password")] [Display(Name = "Confirm Password")] public string PasswordConfirm { get; set; } } public partial class ContactPhone { [Key] public int PhoneID { get; set; } public int ContactID { get; set; } public string Number { get; set; } public string Type { get; set; } } </code></pre> <p><strong>Member Edit Controller</strong></p> <pre><code> private NCOWW_DB db = new NCOWW_DB(); //dbContext public ActionResult Edit(Member member) { try { if (ModelState.IsValid) { member.ModifiedBy = CurrentUser; member.ModifiedOn = DateTime.UtcNow; db.Entry(member).State = EntityState.Modified; &lt;--ERROR OCCURS HERE! db.SaveChanges(); //RETURN TO DASHBOARD return RedirectToAction("Index"); } } catch (Exception err) { /*catch error code*/ } return View(member); } </code></pre> <p><strong>UPDATE</strong></p> <p>Based on the answer below, I have modified my code in an attempt to remove the ill-placed <code>NCOWW_DB db = new NCOWW_DB()</code>. However, this does not solve the problem as I am unsure how to pass the <code>dbContext</code> properly. Regardless, this is where the code currently stands:</p> <p><strong>UPDATED MEMBER MODEL</strong></p> <pre><code> ... private ICollection&lt;Contact&gt; _Contacts; public virtual ICollection&lt;Contact&gt; Contacts { get { return MemberFunctions.LoadMemberContacts(MemberID); } set { _Contacts = value; } } private ICollection&lt;Utility&gt; _Utilities; public virtual ICollection&lt;Utility&gt; Utilities { get { return MemberFunctions.LoadMemberUtilities(MemberID); } set { _Utilities = value; } } } public static class MemberFunctions { public static ICollection&lt;Contact&gt; LoadMemberContacts(int memberID) { NCOWW_DB db = new NCOWW_DB(); return (from c in db.Contact where (c.MemberID == memberID &amp;&amp; c.UtilityID == null &amp;&amp; c.Deleted == false) select c).OrderBy(c =&gt; c.FirstName).ToList(); } public static ICollection&lt;Utility&gt; LoadMemberUtilities(int memberID) { NCOWW_DB db = new NCOWW_DB(); return (from u in db.Utility where (u.MemberID == memberID &amp;&amp; u.Deleted == false) select u).OrderBy(u =&gt; u.Name).ToList(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload