user2243747
user2243747

Reputation: 2967

DbConfiguration instance used before 'EFConfiguration' type was discovered

In my MVC4 application I am having reference to my EF model assembly. Everything was working fine. All of a sudden I started getting below error message.

The default DbConfiguration instance was used by the Entity Framework before the 'EFConfiguration' type was discovered. An instance of 'EFConfiguration' must be set at application start before using any Entity Framework features or must be registered in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information.

I am using EF 6. Any idea what could be the reason? I double check with database and its updated and in sync with EF dll.

Update: I am getting this error when I am trying to instantiate Context object

mEntities context = new Entities();

Thanks

Upvotes: 6

Views: 8873

Answers (2)

Emil
Emil

Reputation: 6893

I just want to leave a solution for those having similar error message. As link from Vinicius Paiva, it clearly explains that if you have multiple dbcontex in different dlls, you should reference to your DbConfiguration file if you created one in your code. in my case I had azure functions project referencing on DAL project and only way to get azure functions project is to create partial class and code based DbConfiguration file.

So extending my edmx file DbContext with code below

namespace myApp.Data.Models
{
    [DbConfigurationType(typeof(myDBContextConfig))]
    partial class myDBEntities
    {

        public myDBEntities(string connectionString) : base(connectionString)
        {
        }
    }

      public  class myDBContextConfig : DbConfiguration
        {
            public myDBContextConfig()
            {
                SetProviderServices("System.Data.EntityClient", 
                SqlProviderServices.Instance);
                SetDefaultConnectionFactory(new SqlConnectionFactory());
            }
        }
    }

This DAL library was referencing on an MVC project where we had another edmx instance. So when I run on this project, it was throwing this exception. In order to fix this. you need to extend or add entityFramework node as below in your config file (web.config in this case)

<entityFramework codeConfigurationType="myApp.Data.Models.myDBContextConfig , myApp.Data">
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework> 

Upvotes: 4

Fernando
Fernando

Reputation: 27

May your problem is because you have a class like this sample in you're DAL

public class EfConfiguration : DbConfiguration

In this case you must initialise this before using EF Context in the Global.asax

Rgds

Upvotes: 0

Related Questions