PNR
PNR

Reputation: 597

Entity Framework - Use 2 DBContext for the same Database

I have a solution where I want to add another project with it's own DBcontext, using its own tables, in the same database as the rest of the solution (the 2 DBcontexts are not sharing any tables). But when running the project I am getting the error below:

Additional information: The default DbConfiguration instance was used by the Entity Framework before the 'Configuration' type was discovered. An instance of 'Configuration' must be set at application start before using any Entity Framework features or must be registered in the application's config file.

I already have a big database built in the existing solution, so I don't want to change anything there, if I can avoid that. I have tried to change the ContextKey in the configuration and also tried to use another DB schema for the new project, but I'm still getting the error.

Upvotes: 2

Views: 1999

Answers (2)

Matt
Matt

Reputation: 1

So first of all can we conclude that only one Dbconfiguration can be used.

The attribute option "[DbConfigurationType(typeof(myDbCOnfig))]" only helps the dbContext to use the DbConfiguration from another assembly.

If you have multi assemblies with their own dbContext, you can set "entityFramework codeConfigurationType="[YourDbConfig]"" in your web.config file once and not set DbConfigurationType attribute for every individual dbcontext

Upvotes: 0

Rami A.
Rami A.

Reputation: 10602

The link provided in the first comment to the question by steve-green solves the issue.

From https://msdn.microsoft.com/en-us/data/jj680699#Moving:

... use the config file to specify the DbConfiguration instance to use. To do this, set the codeConfigurationType attribute of the entityFramework section. For example:
<entityFramework codeConfigurationType="MyNamespace.MyDbConfiguration, MyAssembly"> ...Your EF config... </entityFramework>

Upvotes: 1

Related Questions