简体   繁体   中英

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:

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:

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. The Configuration class that had been generated looked fine, so the suggestion in the warning didn't seem relevant.

So the question I have is whether there is any way to configure Migrations so it ignores a specific Context like 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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