EF Code First Migrations数据库迁移

本文通过core-first方式对nopcommerce进行数据库迁移

1.Setting Up EF Migrations in nopCommerce Solution(设置EF Migration)

The first thing you want to do is to enable migrations in your nopCommerce solution. So fire up nopCommerce in Visual Studio, look at Nop.Web project, and open Web.config. You need to add a connection string to your development database. Note that adding the connection string in Web.config doesn’t affect how nopCommerce works, since nopCommerce doesn’t look for connection string in Web.config. (web.config里的connection string如下图)

EF Code First Migrations数据库迁移

Then, open NopObjectContext.cs in Nop.Data. Add a new constructor that points to the name of the connection string that you’ve just added in previous step. NOTE: replace Pro-nopCommerce with the name of your connection string. (打开NopObjectContext.cs文件做如下图修改)

EF Code First Migrations数据库迁移

The next step is to actually enable migrations in the project. If you have not already done so, bring up Package Manager Console. In the “Default Project” drop-down list, select Nop.Data as the project. Please also make sure Nop.Web is selected as the StartUp Project. Now, enter the command “enable-migrations” in Package Manager Console, and hit Enter! Visual Studio will generate a file named “Configurations.cs”; you can safely ignore it, but you need to keep it.(在default project 处选Nop.Data,然后输入enables-migrations,会生成一个Configurations.cs”文件)

EF Code First Migrations数据库迁移

The last step in EF Migrations setup is done by entering the command “add-migration InitialMigration -IgnoreChanges” in Package Manager Console. “InitialMigration” (highligted yellow) is the name you want to give the the current migration, and the “IgnoreChanges” handle is to tell EF Migrations that you want to leave out the current database as-is: that means you want EF to ignore all the existing tables so that no script is generated for the existing tables.(输入“add-migration InitialMigration -IgnoreChanges”)

EF Code First Migrations数据库迁移

As a result, you’ll see a new .cs file generated by Visual Studio that correspond to the migration - InitialMigration - you’ve just added. If you look at the file, it essentially is a blank file, due to the fact that we used the -IgnoreChanges handle in previous step.(上个步骤生成的文件如下图)

EF Code First Migrations数据库迁移

To actually save this migration over to the database, run the command “update-database” in Package Manager Console. Your database is now ready for actual EF Migration tasks!(运行update-database命令)

EF Code First Migrations数据库迁移

2.Adding a Migration and Updating the Database(添加迁移和更新数据库)

Now, suppose we want to link up Blog and Products, where a Product can have multiple BlogPost that talk about the Product itself. We’ll need a one-to-many relationship between Product and BlogPost.

In essence, what we need is a new ProductId field in BlogPost, and a ICollection property in Product. The following screenshot sums up the update we should add to both Product.cs and BlogPost.cs (in Nop.Core project).(现在,假设你想关联Blog和Products,并且一个Product可以有多个BlogPost,即Product和BlogPost存在一对多关系。最后我们需要在BlogPost实体类中添加一个ProductId属性,在Product实体类中添加一个ICollection属性,如下图)

EF Code First Migrations数据库迁移
EF Code First Migrations数据库迁移

Now you have the properties ready on your domain model, you also need to setup the configuration and tell Entity Framework how you want to format the parameter, for example the relationship, and whether a field is optional/required. Open BlogPostMap.cs in Nop.Data, and enter the following lines, where we tell EF to that Product (or ProductId) is an optional property (meaning it can have NULL as value), and a Product can have many BlogPost, and to use ProductId as the foreign key between BlogPost and Product.(现在实例类上的属性已经有了,你需要在map上做一些操作告诉EF这个关系,如下图)

EF Code First Migrations数据库迁移

We now have enough information to instruct Entity Framework to generate the migration, so again, bring up Package Manager Console, and enter the command “add-migration AddProductToBlogPost”.(然后打开Package Manager Console,输入“add-migration AddProductToBlogPost”)

EF Code First Migrations数据库迁移

This command causes Visual Studio to generate a .cs file that looks like the following(会生成一个如下.cs文件):

EF Code First Migrations数据库迁移

To actually generate the SQL script and update the database, run again the command “update-database” in Package Manager Console. Now, to verify that the database is updated correctly, open up BlogPost table in the database, and check that the new field, foreign key and etc are indeed added.(运行update-database,然后检查数据库是否更新)

EF Code First Migrations数据库迁移

Conclusion

Entity Framework Code-first Migration can be a very handy tool if you customize nopCommerce a lot. Like this you don’t need to touch the database when you want to add new fields, tables and etc to the database. Everything can be done from the code!(Entity Framework Code-first Migration可以让你不需要直接操作数据库,直接通过代码的方式实现数据库修改的方式)

Learn up the technique, and have fun coding!

转载:https://www.pronopcommerce.com/using-entity-framework-ef-code-first-migrations-in-nopcommerce-for-fast-customizations

另提供两个类似资料链接以供参考:
http://www.cnblogs.com/libingql/p/3330880.html
http://blog.csdn.net/yareyou/article/details/50553105

个人整体流程梳理:首先改实体类,然后修改映射关系表,add-migration,最后update-database
数据库版本回溯:
Update-Database –TargetMigration:”201309201643300_AddCity.cs”