Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The default DbConfiguration instance was used before the 'EntityFrameworkConfiguration' type was discovered

 public class EntityFrameworkConfiguration : DbConfiguration
    {
        public EntityFrameworkConfiguration()
        {
            this.SetModelCacheKey(ctx => new EntityModelCacheKey((ctx.GetType().FullName + ctx.Database.Connection.ConnectionString).GetHashCode()));
        }
    }

To make the above code work i have added below line in web.config

But for other project where i am using the assembly reference i am getting exception:

{"The default DbConfiguration instance was used by the Entity Framework before the 'EntityFrameworkConfiguration' type was discovered. An instance of 'EntityFrameworkConfiguration' 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."}

like image 557
Rohit Sindhu Avatar asked Aug 24 '15 08:08

Rohit Sindhu


2 Answers

Your question doesn't state how you are using this custom DbConfiguration.

You could get this error a couple of different ways.

Configuration style setup

as described here: Entity Framework Config File Settings

Configuration as code

as described here: Entity Framework Code-Based Configuration (EF6 onwards) You can hack at this style by doing things like DbConfiguration.SetConfiguration(xxxx). I didnt find this useful at all.

What this really comes down to is how you construct your DbContext.

Configuration file style constructors https://github.com/aspnet/EntityFramework6/blob/master/src/EntityFramework/DbContext.cs#L75 With no arguments - EF6 is uses the configuraiton files to determine the right DbCofniguration to use

with some "connection string-like" arguments again EF6 is using configuration files to determine the DbConfiguration

no config, or bad config - and you will get this sort or exception

Configuration as Code style constructors https://github.com/aspnet/EntityFramework6/blob/master/src/EntityFramework/DbContext.cs#L139

I think this yields better control.

Attribute your DbContext, then use a manually created DbConnection

public class EntityFrameworkConfiguration : DbConfiguration
{
    public EntityFrameworkConfiguration()
    {
        this.SetModelCacheKey(ctx => new EntityModelCacheKey((ctx.GetType().FullName + ctx.Database.Connection.ConnectionString).GetHashCode()));
    }
}

[DbConfigurationType(typeof(EntityFrameworkConfiguration))]
public class MyContext : DbContext
{
    public MyContext(DbConnection existingConnection, bool contextOwnsConnection) 
        : base(existingConnection, contextOwnsConnection)
    { }


    public DbSet<Stuff> Stuff { get; set; }
}


using(var conn = new SqlConnection(asqlserverConnectionString))
            using (var db = new MyContext(conn, true))
            {
                var value = await db.Stuff.Where(s => s.xxx.Equals(primaryKey)).Select(s => new { s.BinaryContent } ).SingleOrDefaultAsync();
            }
like image 131
BozoJoe Avatar answered Sep 21 '22 18:09

BozoJoe


If you are using Code-based configuration, try updating the config file thusly:

<entityFramework codeConfigurationType="MyNamespace.MyDbConfiguration, MyAssembly">
    ...Your EF config...
</entityFramework>
like image 3
Vince I Avatar answered Sep 21 '22 18:09

Vince I