Let's say I've extended identity framework dbContext
to build my own, and I've our authenticated controller who get injected with the dbContext
and fetch an entity related with the current ApplicationUser
, entity framework will relate the two entities leading to a server error because of circular references.
We don't want to serialize circular references.
So we create a new dbContext
in the method who istantiate a new dbcontext
and query the unrelated entities, this will work. But this is not testable, we don't want our controller to strictly depends on the dbContext
, we want it to be injected.
So we add a second parameter, in the constructor, unfortunately this will make the system inject the same dbContext
twice, not so useful.
We tried to create a class fakeDbContext
who inherit from dbContext
and add it services and use it, but now we got two dbcontext, who can potentially generate migrations and configurations and errors...
Which is the right way of doing this in the new MVC6 ?
Edit...
I found out that if my controller require an IEnumerable<dbContext>
i get all the object registered as service of that type, so just doubling the part in startup.cs where we add the dbContext
in the service registration area i get two of them...
The drawback here is that i don't know wich one is the virgin one, it looks like it goes in order of registration, but i have no clue, if this will change.
Edit 2 ...
I've created a TransientDbService
class who have just a factory method taking the IserviceProvider
, it use it to get the options to construct the dbContext
, and then expose it. I've registered it as transient, then in the controller i require this service type.
the drawback here is if i'll ever need a third dbContext
i should write more code, more code means errors and maintaning it.
Edit 3 ...
Not having two dbContext at all. The following setting allow me to have no relationships valorized.
Database.ChangeTracker.QueryTrackingBehavior = Microsoft.Data.Entity.QueryTrackingBehavior.NoTracking;
The drawback here is that i can't use my model graph, making everything more complex...