EF6'ModelConfiguration'设置但未发现 [英] EF6 'ModelConfiguration' set but not discovered

查看:126
本文介绍了EF6'ModelConfiguration'设置但未发现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下库:

EntityMODEL.dll (包含POCO类)

EntityDAL.dll [引用EntityMODEL.dll]

EntitySERVICE.dll [引用EntityMODEL.dll和EntityDAL。 dll]

EntityTEST.dll [引用EntitySERVICE.dll和EntityMODEL.dll的单元测试]

I have the following libraries:
EntityMODEL.dll (contains POCO classes)
EntityDAL.dll [references EntityMODEL.dll]
EntitySERVICE.dll [references both EntityMODEL.dll and EntityDAL.dll]
EntityTEST.dll [unit tests with references to EntitySERVICE.dll and EntityMODEL.dll]

EntitySERVICE.dll EntityMODEL.dll 都需要外部引用(例如从 EntityTEST .dll ),这意味着外界不需要引用 EntityDAL.dll 实体框架

The EntitySERVICE.dll and EntityMODEL.dll are all that need to be referenced by the outside world (e.g. from EntityTEST.dll), meaning that the outside world does not need to reference EntityDAL.dll or Entity Framework.

这是我的DbContext从EntityDAL.dll ...

Here is my DbContext from EntityDAL.dll ...

EntityDAL.dll | DbContext

public class FooContext : DbContext
{
    public FooContext()
    {
        this.Configuration.LazyLoadingEnabled = false;
        this.Configuration.ProxyCreationEnabled = false;
    }

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

    // NESTED DbConfiguration
    public class ModelConfiguration : DbConfiguration
    {
        public ModelConfiguration()
        {
            this.SetHistoryContext( .... )
        }
    }
}

运行单元EntityTEST.dll的测试都可以正常工作。

When running unit tests from EntityTEST.dll all works fine.

我有几个这样的软件包 DAL / SERVICE结构)在我的解决方案中,每个处理不同相关的基础实体组。

I have several of these 'packages' (all following the same MODEL/DAL/SERVICE structure) in my solution each dealing with different related groups of underlying entities.

为了协调这些多个实体包的活动,我有一个编排'(或任务)层与以下库:

To coordinate the activity across these multiple Entity 'packages', I have an 'orchestration' (or task) layer with the following libraries:

TaskMODEL.dll [包含POCO类]

TaskSERVICE.dll [引用TaskMODEL.dll,EntitySERVICE.dll和EntityMODEL.dll]

还提供了TaskMODEL.dll类之间的转换EntityMODEL.dll类

TaskTEST.dll [引用TaskSERVICE.dll和TaskMODEL.dll]

TaskMODEL.dll [contains POCO classes]
TaskSERVICE.dll [references TaskMODEL.dll, EntitySERVICE.dll and EntityMODEL.dll]
-- also provides transformations between TaskMODEL.dll classes and EntityMODEL.dll classes
TaskTEST.dll [references TaskSERVICE.dll and TaskMODEL.dll]

现在,当从TaskTEST.dll运行测试(调用TaskSERVICE.dll中的方法,转换然后调用EntitySERVICE.dll)时,会收到以下错误:

Now, when running a test from TaskTEST.dll (which calls methods in TaskSERVICE.dll, which transforms and then calls EntitySERVICE.dll), I get the following error:

... threw exception:<br/>
System.InvalidOperationException: An instance of 'ModelConfiguration'
was set but this type was not discovered in the same assembly as the
'FooContext' context.  Either put the DbConfiguration type in the same
assembly as the DbContext type, use DbConfigurationTypeAttribute on the
DbContext type to specific the DbConfigurationType, or set the
DbConfiguration type in the config file.

在FooContext实例化过程中发生此错误。在FooContext构造函数中放置了一个调试断点,我可以看到当从第一个测试(EntityTEST)输入构造函数时,代码立即下降到ModelConfiguration的构造函数,一切都很好。但是,当从TaskTEST启动测试时,抛出上述错误,而不是丢弃到ModelConfiguration的构造函数。

This error occurs during instantiation of FooContext. Having put a debug breakpoint on the FooContext constructor I can see that when the constructor is entered from the first test (EntityTEST), the code immediately drops down to the constructor of ModelConfiguration and all is well. However, when the test is initiated from TaskTEST the above error is thrown instead of dropping down to the constructor of ModelConfiguration.

从上面的初始代码片段可以看出,ModelConfiguration类嵌套在FooContext下,所以它绝对是在同一个程序集中。此外,当从EntityTEST.dll启动测试时,同一个库的行为很好。只有当有更多的层并且测试从TaskTEST.dll启动时出现问题。由于ModelConfiguration类在同一个程序集中,我没有在任何项目的app.config中提到ModelConfiguration设置。

As you can see from the initial code snippet above ModelConfiguration class is nested under FooContext, so it is definitely in the same assembly. Additionally, the same library behaves fine when the test is initiated from EntityTEST.dll. It is only when there are more layers and the test is initiated from TaskTEST.dll that there is a problem. As the ModelConfiguration class is in the same assembly, I have not mentioned ModelConfiguration settings in the app.config in any of my projects.

1)             EntityTEST > EntitySERVICE > EntityDAL = GOOD
2) TaskTEST > TaskSERVICE > EntitySERVICE > EntityDAL = ERROR

以前有人看到这个吗?

如上所述,我的解决方案中有几个EntitySERVICE / EntityMODEL / EntityDAL组合。每个DAL的ModelConfiguration类包含DLL名称(因此它们并不都称为所有组合中的所有模型配置),所以该错误可以重新定义为:

As mentioned above, I have several EntitySERVICE/EntityMODEL/EntityDAL combinations in my solution. Having played around a bit and naming each DAL's ModelConfiguration class to include the DLL name (so they're not all called ModelConfiguration across all combinations), the error can be restated as:

... threw exception:<br/>
System.InvalidOperationException: An instance of 

    'ModelConfiguration_NOT_THE_FOO_CONFIG'

was set but this type was not discovered in the same assembly as the
'FooContext' context.  Either put the DbConfiguration type in the same
assembly as the DbContext type, use DbConfigurationTypeAttribute on the
DbContext type to specific the DbConfigurationType, or set the
DbConfiguration type in the config file.

换句话说,环境似乎已经从测试中使用的第一个DAL dll加载了ModelConfiguration然后期望为其使用的后续DAL dll找到相同的ModelConfiguration。

In other words, the environment appears to have loaded the ModelConfiguration from the first DAL dll used during the test and then expects to find the same ModelConfiguration for subsequent DAL dlls that it uses.

这是否意味着我们在整个解决方案中只能有一个ModelConfiguration类? / p>

Does this mean that we can only have ONE ModelConfiguration class across the whole solution???

推荐答案

我有同样的问题。

与这里的解释有关:

http://msdn.microsoft.com/en-us/data/jj680699

有些情况下无法将您的DbConfiguration类放在与DbContext类相同的程序集。例如,您可能在不同的程序集中有两个DbContext类。有两个选项用于处理。

There are cases where it is not possible to place your DbConfiguration class in the same assembly as your DbContext class. For example, you may have two DbContext classes each in different assemblies. There are two options for handling this.

第一个选项是使用该配置文件指定要使用的DbConfiguration实例。为此,请设置entityFramework部分的codeConfigurationType属性。例如:

The first option is to use the config file to specify the DbConfiguration instance to use. To do this, set the codeConfigurationType attribute of the entityFramework section. For example:


...您的EF配置...

codeConfigurationType的值必须是装配和命名空间限定您的DbConfiguration类的名称。

...Your EF config... The value of codeConfigurationType must be the assembly and namespace qualified name of your DbConfiguration class.

第二个选项是在您的上下文类中放置DbConfigurationTypeAttribute。例如:

The second option is to place DbConfigurationTypeAttribute on your context class. For example:

[DbConfigurationType(typeof(MyDbConfiguration))]
public class MyContextContext : DbContext
{
}

传递给该属性的值可以是您的DbConfiguration类型 - 如上所示 - 或装配和命名空间限定类型名称字符串。例如:

The value passed to the attribute can either be your DbConfiguration type - as shown above - or the assembly and namespace qualified type name string. For example:

[DbConfigurationType("MyNamespace.MyDbConfiguration, MyAssembly")]
public class MyContextContext : DbContext
{
}

我仍然使用DbConfigurationTypeAttribute

I am still getting that error using the DbConfigurationTypeAttribute

这篇关于EF6'ModelConfiguration'设置但未发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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