Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that this query</p> <pre><code>string thisQueryWillMessThingsUp = context.SaleMethods.Where(m =&gt; m.Id == 2).Single().Name; </code></pre> <p>brings one instance of the SaleMethod entity into the context and then this code</p> <pre><code>context.Entry(method).State = System.Data.EntityState.Modified; </code></pre> <p>attaches <em>a different instance</em> to the context. Both instances have the same primary key, so EF thinks that you are trying to attach two different entities with the same key to the context. It doesn't know that they are both supposed to be the same entity.</p> <p>If for some reason you just need to query for the name, but don't want to actually bring the full entity into the context, then you can do this:</p> <pre><code>string thisQueryWillMessThingsUp = context.SaleMethods.Where(m =&gt; m.Id == 2).AsNoTracking().Single().Name; </code></pre> <p>If what you are tying to do is update an existing entity and you have values for all mapped properties of that entity, then the simplest thing to do is to not run the query and just use:</p> <pre><code>context.Entry(method).State = System.Data.EntityState.Modified; </code></pre> <p>If you don't want to update all properties, possibly because you don't have values for all properties, then querying for the entity and setting properties on it before calling SaveChanges is an acceptable approach. There are several ways to do this depending on your exact requirements. One way is to use the Property method, something like so:</p> <pre><code>var salesMethod = context.SaleMethods.Find(2); // Basically equivalent to your query context.Entry(salesMethod).Property(e =&gt; e.Name).CurrentValue = newName; context.Entry(salesMethod).Property(e =&gt; e.SomeOtherProp).CurrentValue = newOtherValue; context.SaveChanges(); </code></pre> <p>These blog posts contain some additional information that might be helpful:</p> <p><a href="http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx">http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx</a></p> <p><a href="http://blogs.msdn.com/b/adonet/archive/2011/01/30/using-dbcontext-in-ef-feature-ctp5-part-5-working-with-property-values.aspx">http://blogs.msdn.com/b/adonet/archive/2011/01/30/using-dbcontext-in-ef-feature-ctp5-part-5-working-with-property-values.aspx</a></p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COThanks. (It won't let me vote up your response because I'm new.) The problem with this is that I don't know when I am doing a query that precedes a future update. The query may be buried somewhere in the service layer, and then ultimately, when I go to do an update on that same entity that was queried, I hit the error. Is there a way that I can remove the object from the context tracking, or get ahold of the object that is being tracked by the context, and communicate to the context that I am indeed updating this one object in question? I've researched some in this direction, but no luck yet.
      singulars
    2. COIt depends what the intention of the query is. If the query is intended to bring an entity into the context so that it can be updated and saved, which is usual, then you should design for that, possibly by ensuring before you save that the entity is brought in then if it hasn't been brought in before. If the intention of the query is to get some property of the entity from the database for some reason without bringing the entity into the context, then use AsNoTracking on that query as shown above.
      singulars
    3. COTo add to my previous comment, if you want to ensure that the entity has been brought in, then use the Find method before updating properties in the entity and saving. You could also use a query instead of Find--by default the query will only bring in the entity if it is not already in the context. But Find is more efficient.
      singulars
 

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