Programmers are forced to write blog posts every day. It's so hot that I can write a little bit today!
1. EF initializes the database's 4th medium Policy
EF can automatically create databases based on models in the project. Next, let's look at the four policies for database initialization in Entity Framework.
I. // a new database will be created every time a database is run.
Database. SetInitializer <XXXXXContext> (new DropCreateDatabaseAlways <XXXXXContext> ());
Ii. // only run for the first time ~ To create a new database ~Default Method
Database. SetInitializer <XXXXXContext> (new CreateDatabaseIfNotExists <XXXXXContext> ());
Iii. // after the model is modified ~ Running ~ Will create a new database
Database. SetInitializer <XXXXXContext> (new DropCreateDatabaseIfModelChanges <XXXXXContext> ());
4. // use your own database. Please configure your own connection string in web. config. Note that the name of the connection string should be the same as that of the context: XXXXContext.
Database. SetInitializer <Models. musicStoreContext> (null );
Note: Please add the above Code to Global. asax. After adding it, there will be a broken line. Please add EF namespace: using System. Data. Entity;
The instance code is as follows:
Protected void Application_Start ()
{
// Four policies for database initialization of Entity Framework
// Configure the connection string by yourself
Database. SetInitializer <Models. musicStoreContext> (null );
// Create the first time by default
Database. SetInitializer <Models. musicStoreContext> (new CreateDatabaseIfNotExists <Models. musicStoreContext> ());
// Delete the old database and create a new database each time.
Database. SetInitializer <Models. musicStoreContext> (new DropCreateDatabaseAlways <Models. musicStoreContext> ());
// Create a database when the model relationship changes
Database. SetInitializer <Models. musicStoreContext> (new DropCreateDatabaseIfModelChanges <Models. musicStoreContext> (); AreaRegistration. RegisterAllAreas ();
WebApiConfig. Register (GlobalConfiguration. Configuration );
FilterConfig. RegisterGlobalFilters (GlobalFilters. Filters );
RouteConfig. RegisterRoutes (RouteTable. Routes );
BundleConfig. RegisterBundles (BundleTable. Bundles );
AuthConfig. RegisterAuth ();
}
2. After talking about the above four strategies, the following describes EF's agreed priority principles:
EF tries its best to use conventions instead of configurations. If the specific database connection mode is not configured according to the aforementioned four-way method when the program is running, EF will create a connection according to the Conventions, that is, create a database based on the model relationship. EF
The process for creating a new database is as follows:
First, EF will try to connect to the local instance of SQLserverExpress and find the database with the same name as the context: XXXXContext. If EF cannot find this database, EF will create a database according to the Conventions.
Okay, it's not very good. It's a meal! Ele. Me!