简体   繁体   English

EF 代码首先从数据库生成 C# 种子方法

[英]EF Code First Generate C# Seed Method from Database

I have a database that is populated with test data.我有一个填充了测试数据的数据库。 The table relation is complex and therefore I would like to know if I can generate c# code for inserting that data.表关系很复杂,因此我想知道是否可以生成 c# 代码来插入该数据。

Basically I want to export all that data a C# method Seed that will populate it back when code first migration is run.基本上,我想将所有数据导出为 C# 方法 Seed,该方法将在运行代码首次迁移时将其填充回去。

In the package management console, enter the following command:在package管理控制台输入以下命令:

enable-migrations

It will enable the configuration part of EF and includes a Seed method.它将启用 EF 的配置部分并包含一个Seed方法。 If this is not enough there are more options you can explore, like add-migration to add a custom migration.如果这还不够,您可以探索更多选项,例如add-migration以添加自定义迁移。

See this post for details: http://msdn.microsoft.com/en-us/data/jj554735.aspx or http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp.net-mvc-application有关详细信息,请参阅此帖子: http://msdn.microsoft.com/en-us/data/jj554735.aspxhttp://www.asp.net/mvc/tutorials/getting-started-with-ef-using- mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp.net-mvc-application

When using a Code Based Migration , you can query for data, remove it and re-insert it any way you like.使用基于Code Based Migration时,您可以查询数据、删除数据并以您喜欢的任何方式重新插入数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM