Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 817
user2243747 Avatar asked Jul 01 '15 01:07

user2243747


1 Answers

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> 
like image 150
Emil Avatar answered Nov 03 '22 16:11

Emil