简体   繁体   English

在EF4.3.1代码优先迁移期间如何忽略派生的DbContext

[英]How to ignore derived DbContext during EF4.3.1 Code First migration

I am trying to use code-first migrations, but the project that contains my CustomContext also has a derived class, TracingCustomContext which I use when tracing the SQL generated: 我正在尝试使用代码优先的迁移,但是包含我的CustomContext的项目也有一个派生类TracingCustomContext,我在跟踪生成的SQL时会使用它:

DbContext => CustomContext => TracingCustomContext

The problem I have during code-first migrations is that when trying to run 我在代码优先迁移过程中遇到的问题是尝试运行时

Enable-Migrations

in Package Manager Console, this results in the (not unexpected) warning: 在Package Manager控制台中,这将导致(并非意外)警告:

More than one class deriving from DbContext found in the current project.
Edit the generated Configuration class to specify the context to enable migrations for.

In order to get past this message and move on to Add-Migration -Initial, I had to comment out my TracingCustomContext class, then run Enable-Migrations. 为了摆脱此消息并继续执行Add-Migration -Initial,我必须注释掉TracingCustomContext类,然后运行Enable-Migrations。 The Configuration class that had been generated looked fine, so the suggestion in the warning didn't seem relevant. 生成的Configuration类看起来不错,因此警告中的建议似乎并不相关。

So the question I have is whether there is any way to configure Migrations so it ignores a specific Context like TracingCustomContext? 因此,我的问题是是否有任何方法可以配置迁移,因此它会忽略特定的上下文(例如TracingCustomContext)? For example, an attribute to decorate the class, or a configuration setting somewhere? 例如,用于装饰类的属性,还是某个地方的配置设置?

Any ideas gratefully received. 任何想法表示感谢。

As per the error message: 根据错误消息:

Edit the generated Configuration class to specify the context to enable migrations for.

Open the created Configuration.cs class (int the Migrations folder) and you will see: 打开创建的Configuration.cs类(位于Migrations文件夹中),您将看到:

internal sealed class Configuration : DbMigrationsConfiguration</* TODO: put your Code First context type name here */>

Replace /* TODO: put your Code First context type name here */ with the type name of the context (do the same in the Seed method or remove the Seed method if you are not using it) and it should work. 替换/ * TODO:将您的Code First上下文类型名称放在此处* /中,并使用上下文的类型名称(在Seed方法中执行相同操作,或者如果不使用它,则删除Seed方法),它应该可以工作。

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

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