Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity framework, problems updating related objects
    primarykey
    data
    text
    <p>I am currently working on a project using the latest version of Entity Framework and I have come across an issue which I can not seem to solve. </p> <p>When it comes to updating existing objects, I can fairly easily update the object properties ok, until it comes to a property which is a reference to another class.</p> <p>In the below example I have a class called Foo, which stores various properties, with 2 of these being instances of other classes</p> <pre><code>public class Foo { public int Id {get; set;} public string Name {get; set;} public SubFoo SubFoo {get; set} public AnotherSubFoo AnotherSubFoo {get; set} } </code></pre> <p>When I use the below <code>Edit()</code> method, I pass in the object I wish to update and I can manage to get the <code>Name</code> to properly update, however I have not managed to find a way in which to get the properties of the SubFoo to change. For example, if the <code>SubFoo</code> class has a property of <code>Name</code>, and this has been changed and is different between my DB and the <code>newFoo</code>, it does not get updated.</p> <pre><code>public Foo Edit(Foo newFoo) { var dbFoo = context.Foo .Include(x =&gt; x.SubFoo) .Include(x =&gt; x.AnotherSubFoo) .Single(c =&gt; c.Id == newFoo.Id); var entry = context.Entry&lt;Foo&gt;(dbFoo); entry.OriginalValues.SetValues(dbFoo); entry.CurrentValues.SetValues(newFoo); context.SaveChanges(); return newFoo; } </code></pre> <p>Any help or pointers would be greatly appreciated.</p> <p>UPDATE: Based on the comment by Slauma I have modified my method to </p> <pre><code>public Foo Edit(Foo newFoo) { var dbFoo = context.Foo .Include(x =&gt; x.SubFoo) .Include(x =&gt; x.AnotherSubFoo) .Single(c =&gt; c.Id == newFoo.Id); context.Entry(dbFoo).CurrentValues.SetValues(newFoo); context.Entry(dbFoo.SubFoo).CurrentValues.SetValues(newFoo.SubFoo); context.SaveChanges(); return newFoo; } </code></pre> <p>When running this now, I get the error:</p> <blockquote> <p>The entity type Collection`1 is not part of the model for the current context.</p> </blockquote> <p>To try and get around this, I added code to try to attach the <code>newFoo</code> subclasses to the context, but this through an error saying that the <code>ObjectManager</code> already had an entity the same:</p> <blockquote> <p>An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    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.
 

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