EF6 - 无种子运行update-database命令 [英] EF6 - Run Update-Database Command without seeds

查看:1540
本文介绍了EF6 - 无种子运行update-database命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架6和我使用的迁移。我已经创建了使用初始迁移数据库。现在,我已经做了更改,型号和环境发生了变化,我想更新数据库,但... 当我试图再次运行数据库更新命令的种子也运行过了,这带来由于errores一些数据再次插入。

I'm Using Entity Framework 6 and I'm using migrations. I have already created the database using an initial migration. Now I have done changes to the Model and the context has changed, and I want to update the database BUT... When I try to run again the Database-Update command the seeds are also running too, and this bring errores due some data is inserted again.

这是很难相信,EF没有像 - 无 - 种子对于任何简单的选择。我几乎可以安全,其他的ORM一样。

It is hard to believe that EF doesn't have any simple option like -No-Seed for that. I'm almost secure that other ORMs does.

推荐答案

在此页:的数据库初始化和迁移种子的方法的:

借助 种子 上的 配置 类运行每当更新 - 数据库的PowerShell   命令被执行。除非迁移初始化正在使用   在迁移种子方法将不会被执行时你的应用程序   开始。

The Seed method on the Configuration class runs whenever the Update-Database PowerShell command is executed. Unless the Migrations initializer is being used the Migrations Seed method will not be executed when your application starts.

所以,我觉得你没有很多选择这里,迁移种子方法,如果你运行总是会叫更新,数据库命令。我挖,如果存在一个参数,让你指定无法运行种子法此命令,但我冒牌货它不存在。这些都是你可以使用(你可以找到在这个链接):

So, I think you don't have many options here, the migration Seed method always will be called if you run Update-Database command. I was digging if exist a parameter to this command that let you specify not run the Seed method, but I'm fraid it don't exist yet. These are all the parameters you can use (you can find more info in this link):

Update-Database [-SourceMigration <String>] [-TargetMigration <String>] [-Script] [-Force] 
  [-ProjectName <String>] [-StartUpProjectName <String>] [-ConfigurationTypeName <String>] 
  [-ConnectionStringName <String>] [-AppDomainBaseDirectory <String>] [<CommonParameters>]

Update-Database [-SourceMigration <String>] [-TargetMigration <String>] [-Script] [-Force] 
  [-ProjectName <String>] [-StartUpProjectName <String>] [-ConfigurationTypeName <String>] 
  -ConnectionString <String> -ConnectionProviderName <String> 
  [-AppDomainBaseDirectory <String>] [<CommonParameters>]

如果您要执行一个SQL脚本插入到种子方法的数据,你可以使用一个booleam条件,以避免在第一时间之后,重新插入相同的字段。

If you are executing a sql script to insert data in the Seed method, you could use a booleam condition to avoid reinsert the same fields after the first time.

作为aditional的信息,在您的要求有一个参数,以避免执行种子方法,当你运行更新 - 数据库命令已经在此存在<一href="https://data.uservoice.com/forums/72025-entity-framework-feature-suggestions/suggestions/5980318-update-database-without-seeding"相对=nofollow>网站,这是由EF团队收集来自社区的建议。

As an aditional info, your request in have a parameter to avoid execute the Seed method when you run the Update-Database command already exist in this site, which is used by EF team to collect suggestions from the community.

这篇关于EF6 - 无种子运行update-database命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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