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