Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Identity.EntityFramework OnModelCreating called

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?

like image 357
Alex Avatar asked Feb 01 '17 14:02

Alex


People also ask

How do you call OnModelCreating again?

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.

What is OnModelCreating in Entity Framework?

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.

How often is OnModelCreating called?

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.

Where do you override OnModelCreating?

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.


2 Answers

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()) {
    ...
}
like image 171
JamesT Avatar answered Oct 04 '22 08:10

JamesT


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.

like image 40
awh112 Avatar answered Oct 04 '22 08:10

awh112