I am using Entity Framework 6 with a tenant-shared application. I identify the tenant by looking at the host name of the request, then use it throughout the application, set it to records owned by the tenant, etc.
Each context is located at the end of the request. However, since tenant requests are so frequent, I actually do them only once for the host name, and then put the objects in a read-only dictionary in memory.
The problem is that if you do nothing, you will have as many duplicate tenant records as you have requests (until the thing starts throwing because of now ambiguous requests).
At first I solved this by adding a call to DbSet.Attach () in my data warehouse constructor and connecting the current tenant. However, if you have multiple requests at the same time, you get an exception informing you that you cannot have the same object tied to multiple contexts: "An entity object cannot reference multiple instances of IEntityChangeTracker." Since I sometimes run this on my dev machine, visiting pages too quickly, I cannot imagine that this is suitable for production.
I tried to make a difference by adding this call before saving:
Context.ChangeTracker.Entries<Tenant>().Single().State = EntityState.Unchanged;
Well, that doesn't work either. I get the error message "The relationship between two objects cannot be determined because they are bound to different ObjectContext objects."
... ? , ( SQL), , PK.
, , EF4, Detach, , , DbSet. , . ?
edit: , , , , - . - .