0

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.

1

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer Pawel. All sorted now. That'll teach me to ready the error messages more carefully!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.