简体   繁体   English

Database.SetInitializer中的DropCreateDatabaseIfModelChanges

[英]DropCreateDatabaseIfModelChanges in Database.SetInitializer

I am using code-first pattern in entity framework V4.1. 我在实体框架V4.1中使用代码优先模式。 One of the things I came across which looks really weird is that whenever you make any changes in the model the database is just dropped and created new. 我遇到的其中一件看起来非常奇怪的事情是,无论何时在模型中进行任何更改,数据库都会被删除并创建新的数据库。 This is very untidy because everytime dropping and recreating the database is not ideal solution. 这非常不整洁,因为每次删除和重新创建数据库都不是理想的解决方案。 All my test data and everything gets lost. 我的所有测试数据和一切都丢失了。 I know there is a method named Seed and you can put test data records to insert there. 我知道有一个名为Seed的方法,您可以将测试数据记录插入其中。 However there may be few 100s of test records and it's not viable to put every single record in seed method. 但是,可能只有几百个测试记录,并且将每个记录放在种子方法中是不可行的。 Is there any other alternative to this? 还有其他替代方案吗? I wouldn't have used code-first at all but it is mandatory to use it in project. 我根本不会使用代码优先,但必须在项目中使用它。

您可以将EF设置为仅尝试修改数据库(如果尚不存在)。

Database.SetInitializer<YourDataContext>(new CreateDatabaseIfNotExists<YourDataContext>());

No there is currently no other method and EF itself will probably never support anything more then drop / recreate. 不,目前没有其他方法,EF本身可能永远不会支持任何更多,然后删除/重新创建。 The way ADO.NET team chose is using separate tool for database upgrade called Migrations . ADO.NET团队选择的方式是使用名为Migrations的单独数据库升级工具。 Migrations are currently available only as CTP = they are not suitable for real usage but for testing and collecting feedback back to EF team. 迁移目前仅作为CTP提供=它们不适合实际使用,但用于测试和收集反馈给EF团队。

As a workaround you can have external data population script and call it from seed - it will be much easier for maintenance. 作为一种解决方法,您可以使用外部数据填充脚本并从种子中调用它 - 这将更容易进行维护。

暂无
暂无

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

相关问题 Database.SetInitializer是否不播种数据库? - Database.SetInitializer is not seeding the database? 放置Database.SetInitializer的地方 - Place to put Database.SetInitializer 使用Database.SetInitializer避免Shotgun手术 - Avoiding Shotgun Surgery with Database.SetInitializer Database.SetInitializer(new MigrateDatabaseToLatestVersion <Context, Configuration> ()); 错误 - Database.SetInitializer(new MigrateDatabaseToLatestVersion<Context, Configuration>()); Error Database.SetInitializer的目的 <ArContext> (null)在存储库中? - purpose of Database.SetInitializer<ArContext>(null) inside of repository? 为什么EF4.1 CodeFirst即使没有“ Database.SetInitializer &lt;&gt;()”调用也会创建数据库? - Why is EF4.1 CodeFirst is creating a database even without a `Database.SetInitializer<>()` call? 我可以将我的Database.SetInitializer调用放入具有基本字符串而不是静态构造函数的上下文构造函数中吗? - Can I put my Database.SetInitializer call in the context constructor with the base string instead of a static constructor? 在DropCreateDatabaseIfModelChanges删除数据库之前进行备份 - Make a backup before the database is dropped by DropCreateDatabaseIfModelChanges WebSecurity.InitializeDatabaseConnection,SetInitializer和Database.Initialize - WebSecurity.InitializeDatabaseConnection, SetInitializer and Database.Initialize 在 MySql.Data.EntityFrameworkCore 中找不到数据库 SetInitializer - Database SetInitializer is not found in MySql.Data.EntityFrameworkCore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM