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

查看:58
本文介绍了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 Entity Framework .

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.

这是来自EntityDAL.dll的我的DbContext ...

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.

我的解决方案中有几个这样的程序包"(都遵循相同的MODEL/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

有人以前见过这个陷阱吗?

Anybody seen this gotcha before?

如上所述,我的解决方案中有几种EntitySERVICE/EntityMODEL/EntityDAL组合.反复尝试并命名每个DAL的ModelConfiguration类以包含DLL名称(因此在所有组合中它们都不全称为ModelConfiguration),该错误可以重述为:

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类?

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

推荐答案

根据Entity Framework文档,配置是在应用程序级别全局定义的,然后传播到每个已加载的程序集: http://go.microsoft.com/fwlink/?LinkId=260883

According to documentation on Entity Framework, configuration is defined globally on application level and then propagates to every loaded assembly: http://go.microsoft.com/fwlink/?LinkId=260883

如果您有多个装配体,每个装配体中都定义了单独的配置,则将仅全局使用第一个加载的装配体中的配置.所有其他配置都将被忽略,并以对第一个加载的配置的全局引用替换.然后,它会传播到其他所有已加载的程序集.

If you have several assemblies with separate configurations defined in each of them, then only configuration from the first loaded assembly will be used globally. All other configurations will be ignored and substituted with a global reference to the first loaded configuration. It then propagates to every other loaded assembly.

如果在不同的程序集中有多个DBCotntext类,则它们不能为每个程序集定义本地配置.相反,调用应用程序应该定义自己的配置,并为所有配置进行如下设置:

If you have several DBCotntext classes in different assemblies, they must not define local configurations per assembly. Instead, calling application should define own configuration and set it for all of them as follows:

  public class MyConfiguration : DbConfiguration
  {
    public ReporsitoryConfiguration()
    {
      // your code here
    }
  }

然后:

DbConfiguration.SetConfiguration(new MyConfiguration());

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

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