Today I'm going to talk about EntityFramework 6.0+, which is different from the 4.0 I learned before, and since the release of 4.1, code first has been loved by many people, and Dbcontext APIs have been born. As early as the school time to listen to the teacher said DB First,model first and code first, not how to notice, recently I jumped directly from DB first to Code first, to understand the next, personally think Model first and code first not much Big difference, good English people look at Http://stackoverflow.com/questions/5446316/code-first-vs-model-database-first, here I also slightly explained, code First, the best advantage is that the code is clean and concise, you can use the code to fully control database-related operations, without the need to go to the management system to build a library to build a table, nor those EntityFramework 4.0 inside the auto-generated XML files (looking at the headache, because it is not the code to write), You have to rebuild the Entity Framework (which is really troublesome) after changing the table. But any powerful thing has its drawbacks, and because it is so simple to use, it is easy to lose data, especially if someone like me who jumps directly from DB Frist to code first does not know anything, go to work with people who have already used EF6. That's not the point.FocusIsMigration(Database migration), database migration is normal for enterprise applications. Take our ABP example, first go to the ABP website to download the project Https://github.com/aspnetboilerplate, double-click the NuGet package to manage the. exe, open the project with vs2012+, and find it in the VS toolbarTools= = "NuGet Package Manager= = "Package Manager ConsoleFirstNoteLook at the console above the default item is not entityframework layer, if not then select it, change to the following
In the console input:install-package entityframework command, so the latest version of EF installed on your entityframework layer, is not very simple, and then build the table, Change your profile first of course not change the words are OK, but not safe, the example is to trust the connection, that is, do not need a user name and password can be entered into your database, and then to enter the command time, if the command is not input error, find 3 points : 1. The default item is not entityframework,2. The startup project is not the UI layer, 3. If the previous two are all right, then turn off vs, reopen the project, let vs reload the project, don't ask me why, I don't know (what the big God told me), I can only guess. Pay attention to what I draw with the red line, and wait to tell you why, so the table is built, and the people table is generated from the person class in our example,
See that table attribute, you know why the generated name is people, and all the entity class with the table attribute, the corresponding table is generated when the Update-database command is run, its implementation principle is reflection, as to what the seed method just means,
Open the Migration folder, see the inside of the Configuration.cs, the seed method on the left, each update to the database at the end will execute this method, you can write code here to add records and so on, finally to say that the migration, First see above that Configuration.cs inside of the configuration method inside there is a automaticmigrationenable attribute, at this time it is false, that is, this is not allowed to automatically migrate, of course, this migration folder is added before Go, we first remove it,, no, run the command Add migration initialcreate, see no, error message is not found in the migration configuration, this time we need to start the migration, execute command: enable-migrations, Migration folder and magically appeared, and then run: add-migration initialcreate command, pay attention to that initialcreate,
See the Migration folder on the right is a 2015 more ... Initialcreate, see here understand, and then I posted a picture I found from the Internet, this command I have not used, but there should be no problem. Demo project to GitHub inside to download, address: https://github.com/aspnetboilerplate, I am also a beginner, have problems to discuss together, the great god please advise.
ABP Migration (Database migration)