何时在 EF 代码优先迁移方案中调用 Seed 方法? [英] When is the Seed method called in a EF code first migrations scenario?

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

问题描述

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

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;
        }

这个代码来启动种子:

protected override void Seed(Context context)
    {
        try
        {

我的问题是:什么时候调用 Seed 方法?仅当用户执行 update-database 并且用户没有数据库(基本上是新用户)时,或者当具有现有数据库的用户调用 update-database?

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 方法用于用一些起始数据初始化数据库表.每当您运行迁移并更新数据库时,它将运行种子方法.它主要用于测试阶段,您经常需要重新创建数据库并使用示例数据填充数据库表.请通过此链接 http://blog.oneunicorn.com/2013/05/28/database-initializer-and-migrations-seed-methods/ 更多关于代码的解释.

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 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆