何时在EF代码首次迁移方案中调用Seed方法

何时在EF代码首次迁移方案中调用Seed方法

本文介绍了何时在EF代码首次迁移方案中调用Seed方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新项目,有种子数据类:

 内部密封类配置:DbMigrationsConfiguration< ; DAL.Context> 
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}

此代码开始种子:

  protected override void Seed(Context context)
{
try
{
我的问题是:什么时候被调用的种子方法是什么?只有当用户执行 update-database 并且用户没有数据库(基本上是新用户),或者当具有现有数据库的用户调用 update-database

解决方案

种子方法用于初始化数据库表,一些起始数据。无论何时运行迁移并更新数据库,都将运行种子方法。大多数情况下,它在测试阶段使用,您经常需要重新创建数据库并使用示例数据填充数据库表。请浏览此链接,以便更多的代码解释。


I'm new in a project and there is this class for the seed data:

 internal sealed class Configuration : DbMigrationsConfiguration<DAL.Context>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }

And this code to start the seed:

protected override void Seed(Context context)
    {
        try
        {

My question is: when is the Seed method called? Only when a user does update-database and the user doesn't have the database (basicly a new user), or also when the user with an existing database calls an update-database?

解决方案

Seed method is used to initialized the database tables with some starting data. Whenever you run the migration and update the database it will run the seed method. Mostly it is used during the testing phase where you often need to recreate the database and populate database tables with sample data. Please go through this link http://blog.oneunicorn.com/2013/05/28/database-initializer-and-migrations-seed-methods/ for more explaination on code first.

这篇关于何时在EF代码首次迁移方案中调用Seed方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 00:44