将种子数据放在EF代码优先的OnModelCreating方法中是否可以接受? [英] Is it acceptable to put seed data in the OnModelCreating method in EF code-first?

查看:475
本文介绍了将种子数据放在EF代码优先的OnModelCreating方法中是否可以接受?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用EF 4.3中的迁移API来做种子数据。我一直在玩这个整夜。然而,我的最终目标是让我的项目到用户可以从源代码控制中提取出来,然后按F5,他们很好,数据库,种子数据和所有的。



目前的代码优先在构建新的构建数据库方面做得非常出色,但是在包管理器控制台中执行Update-Database之前,不会插入种子数据。在这一点上,它运行种子方法并插入我的种子数据。


  1. 只需在OnModelCreating方法中执行此操作, li>
  2. 我还可以在这里利用AddOrUpdate扩展方法吗?

  3. 每次按F5时,这个种子数据是否会运行?如果是,是否可以检测数据库是否已经创建,并且只能在创建初始数据库时添加种子数据?


解决方案


  1. 不。每次启动模型定义时,每次启动应用程序并首次使用数据访问时,都会执行 OnModelCreating 。在开发环境中可能会很好,但在生产中却不太好。此外,在构造时您不能使用上下文,因此如何在此方法中种子数据的唯一方法是直接使用SQL。

  2. AddOrUpdate 主要用于迁移。它有隐藏的成本(反映和查询到数据库),所以仔细使用它。

  3. 您可以通过手动执行SQL查询来检测它(您不能在 OnModelCreating中使用 Database.Exists code> - 再次因为在构建上下文时这种方法不可用),但它不属于 OnModelCreating =>单个责任模式。

有更好的方法来做到这一点。




  • MigrateDatabaseToLatestVersion EF 4.3中的数据库初始值设置将运行您的迁移集,存在或更新现有数据库

  • 您可以在应用程序引导时使用 DbMigrator 类,并将数据库迁移到最后一个版本,对整个过程的更高的控制

  • 我没有检查是否可以,但是您应该能够修改MSBuild或添加预构建操作到您的项目,这将以某种方式使用电源shell命令或执行自定义控制台应用程序使用 DbMigrator 类。


I know how to do seed data with the migrations API in EF 4.3. I've been playing with that all night. However, my ultimate goal is to get my project to the point where a user can pull it from source control and press F5 and they are good to go, database, seed data, and all.

Currently code-first does an excellent job of building the DB on a fresh build but seed data isn't inserted until I do Update-Database in the package manager console. At that point it runs the seed method and inserts my seed data.

  1. Is it okay to just do that in the OnModelCreating method?
  2. Can I still take advantage of the AddOrUpdate extension method here?
  3. Will this seed data be run every time I press F5? If so, can I detect if the DB has already been created or not and only add this seed data on initial database creation?

解决方案

  1. No. OnModelCreating is executed every time the model definition must be built = every time you start the application and use data access for the first time. It may be nice in your development environment but it is not nice in production. Moreover you cannot use context while it is constructed so the only way how to seed data in this method is using SQL directly.
  2. AddOrUpdate is mostly for migrations. It has hidden costs (reflection and query to database) so use it carefully.
  3. You can detect it by manually executing SQL query (you cannot use Database.Exists inside OnModelCreating - again because this method is not available when context is being constructed) but it is something that doesn't belong to OnModelCreating => single responsibility pattern.

There are better ways how to do this.

  • MigrateDatabaseToLatestVersion database initializer in EF 4.3 will run your migration set if database doesn't exist or update existing database
  • You can use DbMigrator class at bootstrap of your application and migrate your database to the last version with the higher control over whole process
  • I didn't check if it is possible but you should be able to modify MSBuild or add pre build action to your project which will either somehow use power shell commands or execute custom console application using DbMigrator class.

这篇关于将种子数据放在EF代码优先的OnModelCreating方法中是否可以接受?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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