This method is called when the model for the derived context has been initialized, but before the model has been locked and used to initialize the context.
Typically, this method is called only once when the first instance of the derived context is created. The model for this context is then cached and intended for all additional context instances in the application domain. This caching can be disabled by setting the ModelCaching property to this ModelBuidler, but note that this can seriously degrade performance.
See MSDN
If the database does not exist, it uses the information from the compiled model to create it. A model is created only once for each application. OnModelCreating will never be called when using the Database First approach. It will never be called because all mappings already exist in EDMX, so Code First and DbModelBuilder are never used.
Try calling a static initializer before calling SetInitializer:
using (var context = new TestContext()) { Database.SetInitializer(new CreateDatabaseIfNotExists<EntityContext>()); context.Database.Initialize(true); }
Roman Marusyk Oct 12 '15 at 8:21 2015-10-12 08:21
source share