2

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.

6
  • 1
    Try this: msdn.microsoft.com/en-us/data/jj680699#Moving Commented Dec 11, 2015 at 13:55
  • 1
    Thanks, I tried setting the codeConfigurationType in web.config and that solved my problem :-) Commented Dec 14, 2015 at 6:49
  • @SteveGreene, that should be posted as an answer. ;) Commented Dec 15, 2016 at 23:48
  • @RamiA. - I disagree - the problem described by the OP is indicating the instance of 'Configuration' must be set before using EF. The article in the link by Steve Green does not describe how to set the instance. Commented May 11, 2017 at 20:42
  • @barrypicker, I've isolated the solution in the answer I just posted. Hope that helps you! Commented May 11, 2017 at 22:39

2 Answers 2

1

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>

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.