简体   繁体   English

为什么EF自动运行种子方法?

[英]Why is EF automatically running the seed method?

We use ASP .NET to create API-driven CMSes. 我们使用ASP .NET创建API驱动的CMS。 Occasionally we need to fill the database with default data and use EF's seed method for this purpose. 有时,我们需要使用默认数据填充数据库,并为此使用EF的种子方法。 I understand that seed is called when we run update-database in the Nuget Package Manager console and also when we deploy, but for some reason our seed method seems to be calling itself once the project has been deployed on the server. 我知道在Nuget软件包管理器控制台中运行更新数据库以及在部署时都会调用种子,但是由于某种原因,一旦在服务器上部署了项目,我们的种子方法似乎就会调用自身。 We use an Azure App Service to host our CMSes. 我们使用Azure应用服务托管CMS。

Does anyone know how to turn this auto-seeding behavior off? 有谁知道如何关闭这种自动播种行为? We haven't noticed a defined pattern in terms of timing, only noticing that it's happened after our database has been reinitialized. 我们没有注意到在时序方面已定义的模式,只是注意到它是在数据库初始化后才发生的。

I think that it has to do something with the migrations, however i don't know why you're updating the database manually, you could use migrations for that matter. 我认为它必须与迁移有关,但是我不知道为什么要手动更新数据库,因此可以使用迁移。

1- Go to Nuget Package Manager console. 1-转到Nuget软件包管理器控制台。

2- Enable-Migrations. 2-启用迁移。

3- Set the configuration method to Automatic migrations and data loss. 3-将配置方法设置为自动迁移和数据丢失。

public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
    }

4- Leave the seed method empty 4-将种子方法留空

protected override void Seed(Yourproject.DAL.Yourdbcontext)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data.
    }

Try this if it's what you're looking for. 如果这是您想要的,请尝试此。

Good luck 祝好运

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

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