Entity FrameWork initializes the four-medium policy of the database

Source: Internet
Author: User

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!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.