简体   繁体   English

EF6'ModelConfiguration'设置但未被发现

[英]EF6 'ModelConfiguration' set but not discovered

I have the following libraries: 我有以下库:
EntityMODEL.dll (contains POCO classes) EntityMODEL.dll (包含POCO类)
EntityDAL.dll [references EntityMODEL.dll] EntityDAL.dll [引用EntityMODEL.dll]
EntitySERVICE.dll [references both EntityMODEL.dll and EntityDAL.dll] EntitySERVICE.dll [引用EntityMODEL.dll和EntityDAL.dll]
EntityTEST.dll [unit tests with references to EntitySERVICE.dll and EntityMODEL.dll] EntityTEST.dll [单元测试引用EntitySERVICE.dll和EntityMODEL.dll]

The EntitySERVICE.dll and EntityMODEL.dll are all that need to be referenced by the outside world (eg from EntityTEST.dll ), meaning that the outside world does not need to reference EntityDAL.dll or Entity Framework . EntitySERVICE.dllEntityMODEL.dll都需要外部引用(例如来自EntityTEST.dll ),这意味着外部世界不需要引用EntityDAL.dllEntity Framework

Here is my DbContext from EntityDAL.dll ... 这是来自EntityDAL.dll的我的DbContext ...

EntityDAL.dll | EntityDAL.dll | DbContext 的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( .... )
        }
    }
}

When running unit tests from EntityTEST.dll all works fine. 从EntityTEST.dll运行单元测试时一切正常。


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. 在我的解决方案中,我有几个这样的'包'(都遵循相同的MODEL / DAL / SERVICE结构),每个包处理不同的相关底层实体组。

To coordinate the activity across these multiple Entity 'packages', I have an 'orchestration' (or task) layer with the following libraries: 为了协调这些多个实体'包'的活动,我有一个带有以下库的'业务流程'(或任务)层:

TaskMODEL.dll [contains POCO classes] TaskMODEL.dll [包含POCO类]
TaskSERVICE.dll [references TaskMODEL.dll, EntitySERVICE.dll and EntityMODEL.dll] TaskSERVICE.dll [引用TaskMODEL.dll,EntitySERVICE.dll和EntityMODEL.dll]
-- also provides transformations between TaskMODEL.dll classes and EntityMODEL.dll classes - 还提供TaskMODEL.dll类和EntityMODEL.dll类之间的转换
TaskTEST.dll [references TaskSERVICE.dll and TaskMODEL.dll] TaskTEST.dll [引用TaskSERVICE.dll和TaskMODEL.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: 现在,当从TaskTEST.dll(调用TaskSERVICE.dll中的方法,它转换然后调用EntitySERVICE.dll)运行测试时,我收到以下错误:

... 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.

This error occurs during instantiation of FooContext. 在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. 在FooContext构造函数上放置调试断点后,我可以看到,当从第一个测试(EntityTEST)输入构造函数时,代码会立即下降到ModelConfiguration的构造函数,一切都很好。 However, when the test is initiated from TaskTEST the above error is thrown instead of dropping down to the constructor of ModelConfiguration. 但是,当从TaskTEST启动测试时,将抛出上述错误,而不是下降到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. 正如您从上面的初始代码片段中看到的,ModelConfiguration类嵌套在FooContext下,因此它肯定在同一个程序集中。 Additionally, the same library behaves fine when the test is initiated from EntityTEST.dll. 此外,从EntityTEST.dll启动测试时,相同的库表现正常。 It is only when there are more layers and the test is initiated from TaskTEST.dll that there is a problem. 只有当有更多层并且从TaskTEST.dll启动测试时才会出现问题。 As the ModelConfiguration class is in the same assembly, I have not mentioned ModelConfiguration settings in the app.config in any of my projects. 由于ModelConfiguration类在同一个程序集中,我没有在任何项目的app.config中提到ModelConfiguration设置。

Summary 摘要

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

Anybody seen this gotcha before? 有人看过这个问题吗?

UPDATE UPDATE

As mentioned above, I have several EntitySERVICE/EntityMODEL/EntityDAL combinations in my solution. 如上所述,我的解决方案中有几个EntitySERVICE / EntityMODEL / EntityDAL组合。 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: 玩了一下并命名每个DAL的ModelConfiguration类以包含DLL名称(因此它们并非所有组合都称为ModelConfiguration),错误可以重述为:

... 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.

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. 换句话说,环境似乎已经从测试期间使用的第一个DAL dll加载了ModelConfiguration,然后期望为它使用的后续DAL dll找到相同的ModelConfiguration。

Does this mean that we can only have ONE ModelConfiguration class across the whole solution??? 这是否意味着我们在整个解决方案中只能有一个ModelConfiguration类?

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 根据有关实体框架的文档,配置在应用程序级别全局定义,然后传播到每个加载的程序集: 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. 然后它传播到每个其他加载的程序集。

If you have several DBCotntext classes in different assemblies, they must not define local configurations per assembly. 如果在不同的程序集中有多个DBCotntext类,则它们不能为每个程序集定义本地配置。 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
    }
  }

and then: 然后:

DbConfiguration.SetConfiguration(new MyConfiguration());

I am having the same issue. 我有相同的问题。

Seems like the answer related to the explanation here: 似乎与这里的解释相关的答案:

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

There are cases where it is not possible to place your DbConfiguration class in the same assembly as your DbContext class. 在某些情况下,无法将DbConfiguration类放在与DbContext类相同的程序集中。 For example, you may have two DbContext classes each in different assemblies. 例如, 您可能在不同的程序集中有两个DbContext类。 There are two options for handling this. 处理此问题有两种选择。

The first option is to use the config file to specify the DbConfiguration instance to use. 第一个选项是使用配置文件指定要使用的DbConfiguration实例。 To do this, set the codeConfigurationType attribute of the entityFramework section. 为此,请设置entityFramework部分的codeConfigurationType属性。 For example: 例如:

...Your EF config... The value of codeConfigurationType must be the assembly and namespace qualified name of your DbConfiguration class. ...您的EF配置... codeConfigurationType的值必须是DbConfiguration类的程序集和名称空间限定名称。

The second option is to place DbConfigurationTypeAttribute on your context class. 第二个选项是将DbConfigurationTypeAttribute放在上下文类中。 For example: 例如:

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

The value passed to the attribute can either be your DbConfiguration type - as shown above - or the assembly and namespace qualified type name string. 传递给属性的值可以是DbConfiguration类型(如上所示),也可以是程序集和名称空间限定类型名称字符串。 For example: 例如:

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

I am still getting that error using the DbConfigurationTypeAttribute 我仍然使用DbConfigurationTypeAttribute获得该错误

There can only be one DBConfiguration type within the same AppDomain. 同一AppDomain中只能有一个DBConfiguration类型。 the attribute works as long as only one is loaded. 只要加载了一个属性,该属性就可以工作。

If you have two context classes with different Configurations, the second one will always fail. 如果您有两个具有不同配置的上下文类,则第二个将始终失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM