EF 4.1 Code First - OnModelCreating call time
Asked Answered
C

3

9

When I debug my application this method seems not to be executing in constructor context of DbContext. So when it is called then?

Cristalcristate answered 12/4, 2011 at 11:20 Comment(0)
T
11

This method is called when EF needs to access database for the first time (so it is not during context instancing). If the database doesn't exists it uses information from compiled model to create it. The model is created only once per application (it is cached internally) so even if you dispose the context your model will be still reused for the next instance.

Tarsus answered 12/4, 2011 at 18:29 Comment(0)
T
32

To be clear, OnModelCreating has nothing to do with whether or not EF is creating the database for you.

In order to interact with the database, Entity Framework must build up a model of the entities that will be persisted. If it doesn't already have that model built and cached, this event fires.

After the model is built, a hash is generated from it, and EF then attempts to connect to the database to see if (a) it exists, (b) it contains version information, a previous hash stored in the EdmMetadata table, and (c) whether or not that hash matches the one previously calculated from the model.

If the hashes do not match, EF uses the database initializer that's been set (by calling Database.SetInitializer(new SomeInitializerType()), which may or may not drop, create, or otherwise modify your database. The default initializer, used when you don't specify a different one, does nothing to your database, and once it has run, Entity Framework will refuse to interact with the database if it is out of sync.

If your database does not contain an EdmMetadata table with a hash in it, Entity Framework assumes you're managing the database schema on your own and happily attempts to make use of it, hoping you've gotten things right. If the schema doesn't match EF's expectations later on down the road, you'll get errors when calling SaveChanges() and such.

In all of these scenarios, whether you're managing your own database schema or letting EF drop and recreate it as needed, whether the database is in sync with your model or not, the OnModelCreating event fires the first time Entity Framework needs to know what it's persisting.

Twentyfourmo answered 31/5, 2011 at 0:35 Comment(0)
T
11

This method is called when EF needs to access database for the first time (so it is not during context instancing). If the database doesn't exists it uses information from compiled model to create it. The model is created only once per application (it is cached internally) so even if you dispose the context your model will be still reused for the next instance.

Tarsus answered 12/4, 2011 at 18:29 Comment(0)
S
-2

OnModelCreating triggers when EF4 is creating the database. Usually this event is used to configure the way EF4 generates the database, so if you executed your application before and your model remains the same, the database is already there and EF4 does not try to generate a new one (although this behaviour also depends on some configuration parameters).

Sprite answered 12/4, 2011 at 11:46 Comment(2)
isn't this configuration should always be executed even in my case (db is not generated when model changes)? otherwise how EF will know how to map columns?Cristalcristate
If the db is not generated, OnModelCreating will not trigger. In the codefirst way, EF uses conventions for all the unspecified mapping. Once the database is created, EF remembers the mapping each time the application is executed. If you are making changes in your model while developing the application, perhaps you should use the "AlwaysRecreateDatabase" option, the database will be regenerated each time you run your app and OnModelCreating will trigger always.Sprite

© 2022 - 2024 — McMap. All rights reserved.