如何防止DbContext更改数据库? [英] How to prevent DbContext from altering the database?

查看:339
本文介绍了如何防止DbContext更改数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Entity Framework(目前使用EF6测试版),我在现有数据库中使用代码第一模式。实体和 DbContext 类是使用T4模板自动创建的。



我想防止 DbContext 从运行时创建/更改任何内容到数据库。



我如何做?

解决方案

执行 enable-migrations 命令时,系统会向您显示文件夹 / Migrations 您可以找到名为 Configuration.cs 的文件。在该文件的构造函数中,默认情况下,有一个属性设置为值:

  AutomaticMigrationsEnabled = false; 

这将确保您的数据库不会自动迁移,而是手动调用每个迁移。



但是,如果你的数据库比你的域模型大,也​​就是说,你只是在已经存在的数据库的一部分上运行,是ASP.NET应用程序, Application_Start 事件处理程序),您需要添加以下代码:

  Database.SetInitializer< YourDbContext>(null); 

否则,Entity Framework会抱怨您的域模型中的数据库定义与实际当前的数据库定义不匹配



如果您想确认YourDbContext不尝试更改数据库,请执行以下操作:

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new YourCustomException(Code first changes are not allowed。);
}

再次编辑



我试图理解你想要完成什么样的场景。如果你有现有的数据库,并且你想手动更新它,这是方法:


  1. 使用DbContext生成器模板生成DbContext和来自现有数据库的实体

  2. 执行enable-migrations

  3. 执行add-migration初始

  4. 步骤3应生成空迁移正确地做了
    。您可以删除它。

  5. 现在,每当您进行某些更改时,您需要执行添加迁移
    ChangeName。这不会去数据库,除非你做
    update-database。


I'm learning Entity Framework (currently using EF6 beta) and I'm using the code first pattern on an existing database. The entities and DbContext class are created automatically using a T4 template.

I would like to prevent the DbContext from creating / altering anything into the database at runtime.

How can I do that?

解决方案

When you do enable-migrations command, you will be presented with folder /Migrations under which you can find file named Configuration.cs. Within constructor of that file, by the default, there is a property set to value:

 AutomaticMigrationsEnabled = false;

Which will ensure that your database won't be migrated automatically, but rather by invoking each migration manually.

However, if your database is larger than your domain models, i.e. you're just operating on the portion of already existing database, then somewhere in your application start (if it is ASP.NET app, Application_Start event handler), you need to add the following code:

Database.SetInitializer<YourDbContext>(null);

Otherwise, Entity Framework will complain that there is a mismatch between database definition in your domain model and actual current state of the database.

EDIT:

If you want to be sure that YourDbContext is not attempting to change database, do this:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    throw new YourCustomException("Code first changes are not allowed."); 
}

EDIT again:

I'm trying to understand what scenario are you trying to accomplish. If you have existing database, and you want to update it but only manually, this is the approach:

  1. Use DbContext generator template to generate DbContext and entities from existing database.
  2. Run enable-migrations
  3. Do add-migration Initial
  4. Step 3 should generate empty migration if everything was done properly. You can delete it.
  5. Now whenever you do some change, you need to do add-migration ChangeName. This will not go to database, unless you do update-database.

这篇关于如何防止DbContext更改数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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