I am working on 2 similar projects - but I did not create either of them.
They both have the same local context, as follows:
using Microsoft.AspNet.Identity.EntityFramework;
public class LocalContext :
IdentityDbContext
<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
//.....
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
AddConfigurations(modelBuilder);
}
}
Searching the entire project for references to OnModelCreating
returns the 2 instances mentioned above, plus the reference to this method in the namespace Microsoft.AspNet.Identity.EntityFramework
These projects are made using Onion architecture, and I am running the REST layer using Swagger.
The problem is that in one of the projects, OnModelCreating
is called when I start the application (Which I would expect), whilst in the other it is called the first time a user makes an action.
Can anyone explain why this is?
I assume there is a setting somewhere that is triggering this call - but I cannot find it - as I said above I have searched the entire project, but cannot find anywhere that is calling this method.
As far as I can see all the references are the same between the 2 projects - can anyone help?
So to get OnModelCreating fired again IDbModelCacheKeyProvider. CacheKey must be changed. To change cache key, DbContext class must implement IDbModelCacheKeyProvider. When new cache key is returned In CacheKey property of IDbModelCacheKeyProvider then OnModelCreating event fires again.
The DbContext class has a method called OnModelCreating that takes an instance of ModelBuilder as a parameter. This method is called by the framework when your context is first created to build the model and its mappings in memory.
OnModelCreating will be called only once that's default behaviour. According to OnModelCreating documentation. Typically, this method is called only once when the first instance of a derived context is created. The model for that context is then cached and is for all further instances of the context in the app domain.
You can override the OnModelCreating method in your derived context and use the ModelBuilder API to configure your model. This is the most powerful method of configuration and allows configuration to be specified without modifying your entity classes.
It's called when your database context is initialized.
If you're using a database initializer in your startup code, it'll happen then:
Database.SetInitializer(new CreateDatabaseIfNotExists<MyDbContext>());
Otherwise it'll happen the first time you create an instance of it:
using (var context = new MyDbContext()) {
...
}
The OnModelCreating
method is typically called when you create the first instance of a derived context or use data access in your application. The reason this is at different points in your applications is probably due to the fact that you call your data access layers in different points in the lifecycle. I would think that in this case, one application creates a copy of the LocalContext
at startup and the other doesn't create a context until a user accesses the data layer.
Take a look here for some detailed documentation and here for another SO question that touches on this topic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With