用于开发和生产的不同种子 [英] Different seed for development and production

查看:57
本文介绍了用于开发和生产的不同种子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据构建配置(调试/发行版),使用Entity Framework(6+)以不同的方式为数据库播种的推荐方法是什么?



现在我是使用MigrateDatabaseToLatestVersion初始化程序。在开发过程中,我喜欢在数据库中存储假数据以进行测试。因此,我在Configuration类的种子方法中创建了此测试数据(启用代码优先)。但是,每次我通过构建服务器发布产品时,我都必须在种子方法中注释很多代码,提交该代码,创建发行版,然后撤消所有注释以继续使用测试数据进行开发。



我想这不是要走的路。因此,希望您能告诉我正确的方法。

解决方案

有很多可能性


  1. 预处理程序指令

一个像你,盖特·阿诺德已经在谈论,使用 #if调试

 受保护的覆盖作废void Seed(BookService .Models.BookServiceContext上下文)
{
#if DEBUG
context.Authors.AddOrUpdate(x => x.Id,
new Author(){ID = 1,名称=测试用户},
);
#else
context.Authors.AddOrUpdate(x => x.Id,
new Author(){ID = 1,名称=生产用户},
) ;
#endif
}




  1. Configuration

另一种方法是在appsettings.json中进行配置,也许您想使用开发数据来设置应用程序,您可以添加以下内容:

  {环境:开发} 

,然后在种子中检查以下内容:

 受保护的重写void Seed(BookService.Models.BookServiceContext上下文)
{
var builder = new ConfigurationBuilder();
builder.AddInMemoryCollection();
var config = builder.Build();

if(config [ environment]。Equals( development))
{
context.Authors.AddOrUpdate(x => x.Id,
new Author(){ID = 1,Name = Test User},
);
}
else if(config [ environment]。Equals( producion))
{
context.Authors.AddOrUpdate(x => x.Id,
new Author(){ID = 1,名称=生产用户},
);
}
}




  1. 环境变量( asp网络核心的解决方案)

(另请参见
和稍后通过DI:

  public void Configure(IApplicationBuilder app,IHostingEnvironment env)
{
if( env.IsDevelopment())
{
SeedDataForDevelopment();
}
}


What is the recommended approach to seed a database differently using Entity Framework (6+) depending on the build configuration (Debug / Release)?

Right now I am using the MigrateDatabaseToLatestVersion initializer. During development I like to have fake data in my database for testing. So I create this test data in the Seed method of the Configuration class (that came with enabling code-first). However, each time I publish the product via build server I have to comment a lot of code inside my seed method, commit this, create the release, and then undo all the comments to continue development with the test data.

I guess this is not the way to go. So I hope you can tell me the proper way to do it.

解决方案

there are many possibilities

  1. Preprocessor directive

One is like you and Gert Arnold already talked about, using the #if DEBUG:

protected override void Seed(BookService.Models.BookServiceContext context)
{
#if DEBUG
    context.Authors.AddOrUpdate(x => x.Id,
        new Author() { Id = 1, Name = "Test User" },
    );
#else
    context.Authors.AddOrUpdate(x => x.Id,
        new Author() { Id = 1, Name = "Productive User" },
    );
#endif
}

  1. Configuration

Another way would be with configuration in the appsettings.json, maybe you want to set up the application with development-data, you can add something like

{ "environment" : "development" }

and in the seed you check for this:

protected override void Seed(BookService.Models.BookServiceContext context)
{
    var builder = new ConfigurationBuilder();
    builder.AddInMemoryCollection();
    var config = builder.Build();

    if (config["environment"].Equals("development"))
    {
        context.Authors.AddOrUpdate(x => x.Id,
            new Author() { Id = 1, Name = "Test User" },
        );
    }
    else if (config["environment"].Equals("producion"))
    {
        context.Authors.AddOrUpdate(x => x.Id,
            new Author() { Id = 1, Name = "Productive User" },
        );
    }
}

  1. Environment variables (solution for asp net core)

(see also https://docs.asp.net/en/latest/fundamentals/environments.html)

You can add an environment variable

and later on via DI:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        SeedDataForDevelopment();
    }
}

这篇关于用于开发和生产的不同种子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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