4

I am using MYSql server as a database behind my windows form application. There are two schemas in my db that I have to throw entries into. I have created two context objects one for each schema. When I use contextA which is on schema1 all the entries are done perfectly but when I use contextB I get this exception. Does it have something to do with MySql Driver.

svick
  • 236,525
  • 50
  • 385
  • 514
Hina
  • 113
  • 1
  • 1
  • 8
  • Are you using the 2 contexts concurrently or `disposing` the first before `using` the other? – haim770 Jun 05 '13 at 08:31
  • Do you have some code to go with this? It could be something as simple as you are getting the entity and working with it outside of EF and then are trying to attach it back, but EF already has a copy cached. – Justin Jun 05 '13 at 13:07
  • @Justin I guess that was the problem I had stored user entity outside EF to maintain session and later to create new entries with second context instead of using its userID I was using whole user object. I fix that and its working thanks – Hina Jun 05 '13 at 17:15

3 Answers3

9

This error says you are trying to attach an entity to your context but its already attached to another one.

My suspicion is that this is probably not a direct reference but perhaps one of the navigation properties on your context contains an entity which is attached to the other context. In my opinion (from what you have described) seperate contexts should only really be used if they are disconnected object structures, eg they have no FKs between the contexts.

The other thing to avoid is to ensure that for each unit of work you only use one instance of each context. If you try and use an entity from another context instance this error will also occur.

EDIT:

IDs are generally a better idea to use if you want to maintain scope outside of the current context. You can reattach entities to EF so you can add them in the way you are describing but you have to make sure the original context is disposed or the entity is detached and then manually attach it to the new context with something like the following:

    public DbEntityEntry<T> EnsureAttachedEF(T entity)
    {
        var e = m_Context.Entry(entity);
        if (e.State == EntityState.Detached)
        {
            m_Context.Set<T>().Attach(entity);
            e = m_Context.Entry(entity);
        }

        return e;
    }

This however is quite a bit of work so using IDs is generally a better idea.

undefined
  • 33,537
  • 22
  • 129
  • 198
  • +1 - Without more info, my guess would be the 2nd part - there are probably 2 contexts. I have had similar issues when attempting to cache entities. – Tim Hobbs Jun 05 '13 at 13:07
4

It is almost certainly caused by proxies and change tracking. Disable these features in both constructors and see if it resolves your issue.

public class contextA : DbContext
{
   public contextA()
   {
      Configuration.ProxyCreationEnabled = false;
   }
}
qujck
  • 14,388
  • 4
  • 45
  • 74
  • 1
    And if it does then what? Are you suggesting he completely disable change tracking to "fix" the issue? IMO that is no fix at all and doesn't really point to the real problem. – Tim Hobbs Jun 05 '13 at 13:05
  • 1
    @jesus.tesh I agree with your points. All I'm doing is identifying the cause of the problem. You don't **need** proxies to track changes but if you are dependent on them then you have to accept you can't add the same instance of an object to 2 contexts. Personally I don't use proxy objects. – qujck Jun 05 '13 at 13:22
2

Moving comment to anwser:

It seems you maybe working with the entity outside of the EF context. This would cause the changes to the entity to not be tracked. At the same time, EF would cache this entity and if you try to attach the entity to the context, it will see it already exists and will throw an error.

You could use the NoTracking option in EF to stop EF from caching the entity if you are working with it outside of the context.

Justin
  • 3,337
  • 3
  • 16
  • 27