Apply EF Code First to a database that already exists

Source: Internet
Author: User

The Entity Framework v6.1 begins native support for applying code first to an existing database. The following is a demo of how to apply code first to an existing database and how to use code first migration to update the database.

1. Apply code first to a database that already exists

First, create the database codefirstfromexistingdb, then create two tables Categories and Products , with the following table structure:

Categories :

Products:

Then insert the test data:


Next, create an MVC project and generate the code First data model from the database:

1. Create a web app named Codefirstexistingdb, and set up authentication as a personal user account

After the web app is created, right-click the project directory, select Add-New Item-Data-ado.net Entity Data model, set name:StoreContext

Click Add to select the code first from the Database on the wizard page

Click Next, click New Connection in the Select Connection screen,

On the Connection Properties page, set the database information:

Click Next, select the Database object (table), and tick the check box to determine the plural form of the generated object name

Ado. NET Entity Data model is created, the product, category, and Storecontext three classes are created in the project root directory.

Then add the controller and view to show the data:

Build the project, then right-click the Controller folder, select Add-Controller, select the MVC5 controller that contains the view (using the entity Framework), and the controller settings are as follows:

Once created, right-click on the index.cshtml under the Products folder under the widget and view it in the browser to see the data displayed on the page.

2. Migrating the update database using code First

Code First migration, you can update the model changes in your code to the database, to use code to migrate, you need to open it:

In the Package Manager console input:enable-migrations-contexttypename codefirstexistingdb.storecontext , enter. (Note that you need to develop the context that you want to use because the context of the authentication was created automatically when the project was created). After execution, the Migrations folder is created and contains a Configuration.cs class.

At this point, you need an extra step to work with code first, and if you create a migration now, it will try to add the entities that are contained in the context to the database, and because the products and categories tables already exist in the database, the migration will fail. So first you need to create a migration that is initialized to NULL, and then we can add migrations to any modifications.

In the Package Manager console, enter:add-migration initialcreate-ignorechanges to create an empty migration.

The ignorechanges logo is used to ensure that this migration will not do anything. Executing it creates a migrations table in the database, and themigrations table is a snapshot of the database before migration.

Next, in the Package Manager console input:update-database is used to update the initialization migration to the database.

After migration initialization is complete, use the code first to migrate the update database:

Add the Description column to the product table and set the length to ([Stringlength ()] ); Now add a new migration to update the changes to the database.

In the Package Manager console input:add-migration add_product_description , after executing the Migrations folder, a file is added to describe the migration. Next, in the Package Manager console, enter:

Update-database , the new columns will be added to the Products table.

Apply EF Code First to a database that already exists

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.