Code first adds a table from an existing database

Source: Internet
Author: User

Describe

Just use EF, still do not understand, encounter the following problem, record.

All say EF good, has been useless, previously written code are ADO, write up time and effort has not much progress, if you can simplify these things to focus on logic or more useful place is not better. So I want to use EF. Code first, from the literal point of view is a database, through the model to create a database, as if only through the model to generate the database, at least I have been in contact for 2 days, the project has been started for some time, the database has a certain amount of data, although the test data, But also do not want to delete, the new addition of data is very annoying. To find a way to use code first without deleting the data table.

Migrations

This tool is very good, but the model has changed when you can use the command to update directly to the database, the Internet has been looking for a long time from the model update to the database, did not mention how to add the existing database tables in the update. Google a day finally saw the method, in fact migrations for us to think of.

Migrations How to use: https://msdn.microsoft.com/zh-cn/data/jj591621

Methods for adding an existing table to migrations: https://channel9.msdn.com/Blogs/EF/Migrations-Existing-Databases

Process

It's actually very simple.

1, using the ADO instance Data Model tool, from the database code first, the required table model is generated into the project, this time the necessary models have come out.

2, I do when the generation of the model DbContext deleted, with my own dbcontext, after all, there are so many DbContext connection function or the same looks very unsightly, the resulting dbset copied to their dbcontext.

3, use add-migrations-ignorechanges, I feel here is the focus, if not enabled migrations enabled, Enable-migrations above has the use method.

4, Update-database this time should be able.

5, if the update Model,add-migrations command, and then update-database on it.

Code first adds a table from an existing 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.