在发现“ EntityFrameworkConfiguration”类型之前使用了默认的DbConfiguration实例 [英] The default DbConfiguration instance was used before the 'EntityFrameworkConfiguration' type was discovered

查看:431
本文介绍了在发现“ EntityFrameworkConfiguration”类型之前使用了默认的DbConfiguration实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

要使上面的代码起作用,我在下面添加了代码在web.config

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:


{默认的DbConfiguration实例在发现'EntityFrameworkConfiguration'类型之前已由Entity
Framework使用。必须设置'EntityFrameworkConfiguration'实例$在使用任何Entity Framework功能之前,在应用程序启动时b $ b或
必须在应用程序的配置文件中注册。请参阅
http://go.microsoft.com/fwlink/?LinkId=260883 以获得更多信息。}

{"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."}


推荐答案

您的问题没有说明您如何使用此自定义DbConfiguration。

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

您可能会得到此错误有两种不同的显示方式。

You could get this error a couple of different ways.

配置样式设置

如此处所述:实体框架配置文件设置

配置为代码

如此处所述:基于实体框架代码的配置(从EF6开始)
您可以 hack 通过执行DbConfiguration.SetConfiguration(xxxx)之类的方式进行。我根本没有发现它有用。

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.

这真正归结为您如何构造DbContext。

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

配置文件样式构造函数
https://github.com/aspnet/EntityFramework6/blob/master/src/EntityFramework/DbContext.cs#L75
不带参数-EF6使用配置文件来确定正确的再次使用DbCofniguration

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

并带有一些类似于连接字符串的参数EF6正在使用配置文件来确定DbConfiguration

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

配置为代码样式构造函数 href = https://github.com/aspnet/EntityFramework6/blob/master/src/EntityFramework/DbContext.cs#L139 rel = noreferrer> https://github.com/aspnet/EntityFramework6/blob/master / src / E ntityFramework / DbContext.cs#L139

我认为这样可以提供更好的控制。

I think this yields better control.

DbContext,然后使用手动创建的DbConnection

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();
            }

这篇关于在发现“ EntityFrameworkConfiguration”类型之前使用了默认的DbConfiguration实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆