Code First: Add a table in an existing database, codefirst

Source: Internet
Author: User

Code First: Add a table in an existing database, codefirst
Description

I just used EF. I haven't figured it out yet. If you encounter the following problems, record it.

EF is easy to use and never used. Previously, all the code written was ADO. NET, it is time-consuming and labor-consuming to write, but it is not better if you can simplify these things and put your energy into logic or more useful places. So I want to use EF. Code First, literally, it means that there is a database after the Code, and the database is created through the Model. It seems that the database can only be generated through the Model, at least for two days, the project has started for a while, and the database already has some data. Although it is test data, it does not want to delete it. It is also annoying to add new data. You can find a way to use Code First without deleting a data table.

Migrations

This tool is very good, but when the Model changes, you can use the command to directly update it to the database. It has been found online for a long time from Model update to database, I didn't mention how to add the tables in the existing database to the update. After one day, Google finally saw the method. In fact, Migrations thought of us.

Migrations usage: https://msdn.microsoft.com/zh-cn/data/jj591621

Method to add an existing table to Migrations: https://channel9.msdn.com/Blogs/EF/Migrations-Existing-Databases

Process

It is actually very simple.

1. Use the ADO. NET instance data Model tool, Code First from the database, to generate the Model of the expected table to the project. At this time, the necessary Model has come out.

2. When I did this, I deleted the DbContext of the generated model and used my own DbContext. After all, there are so many DbContext connection functions that seem to be the same, copy the generated DbSet to your own DbContext.

3. Use Add-Migrations-IgnoreChanges. I think this is the focus. If Migrations is not enabled, Enable-Migrations can be used.

4. Update-Database.

5. If you Update the Model, Add-Migrations command, and then Update-Database.

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.