Nei*_*l W 6 c# entity-framework
我有以下库:
EntityMODEL.dll (包含POCO类)
EntityDAL.dll [引用EntityMODEL.dll]
EntitySERVICE.dll [同时引用EntityMODEL.dll和EntityDAL.dll]
EntityTEST.dll [与EntitySERVICE.dll和EntityMODEL.dll]
该EntitySERVICE.dll和EntityMODEL.dll都需要由外界所引用的(例如,从EntityTEST.dll),这意味着外界不需要参考EntityDAL.dll或实体框架。
这是我来自EntityDAL.dll的DbContext ...
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( .... )
}
}
}
Run Code Online (Sandbox Code Playgroud)
从EntityTEST.dll运行单元测试时,一切正常。
我的解决方案中有几个这样的“程序包”(都遵循相同的MODEL / DAL / SERVICE结构),每个程序包都处理基础实体的不同相关组。
为了协调跨多个“实体”包的活动,我具有一个“编排”(或任务)层,其中包含以下库:
TaskMODEL.dll [包含POCO类]
TaskSERVICE.dll [引用TaskMODEL.dll,EntitySERVICE.dll和EntityMODEL.dll]
-还提供TaskMODEL.dll类和EntityMODEL.dll类之间的转换
TaskTEST.dll [引用TaskSERVICE.dll和TaskMODEL .dll]
现在,当从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.
Run Code Online (Sandbox Code Playgroud)
在实例化FooContext时发生此错误。在FooContext构造函数上放置调试断点后,我可以看到,当从第一个测试(EntityTEST)输入该构造函数时,代码立即下降到ModelConfiguration的构造函数中,一切都很好。但是,当从TaskTEST启动测试时,会引发上述错误,而不是将错误提交给ModelConfiguration的构造函数。
从上面的初始代码片段可以看出,ModelConfiguration类嵌套在FooContext下,因此它肯定在同一程序集中。此外,从EntityTEST.dll启动测试时,同一库的运行情况也很好。仅当存在更多层并且从TaskTEST.dll启动测试时,才出现问题。由于ModelConfiguration类位于同一程序集中,因此我没有在任何项目的app.config中提及ModelConfiguration设置。
1) EntityTEST > EntitySERVICE > EntityDAL = GOOD
2) TaskTEST > TaskSERVICE > EntitySERVICE > EntityDAL = ERROR
Run Code Online (Sandbox Code Playgroud)
有人看过这个陷阱吗?
如上所述,我的解决方案中有几种EntitySERVICE / EntityMODEL / EntityDAL组合。经过反复尝试并命名了每个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.
Run Code Online (Sandbox Code Playgroud)
换句话说,该环境似乎已从测试期间使用的第一个DAL dll中加载了ModelConfiguration,然后期望为其使用的后续DAL dll找到相同的ModelConfiguration。
这是否意味着我们在整个解决方案中只能拥有一个ModelConfiguration类?
根据实体框架的文档,配置是在应用程序级别全局定义的,然后传播到每个加载的程序集: http://go.microsoft.com/fwlink/ ?LinkId=260883
如果您有多个程序集,每个程序集中都定义了单独的配置,则只有第一个加载的程序集中的配置才会被全局使用。所有其他配置将被忽略并替换为对第一个加载配置的全局引用。然后它传播到所有其他加载的程序集。
如果不同程序集中有多个 DBCotntext 类,则它们不得为每个程序集定义本地配置。相反,调用应用程序应该定义自己的配置并为所有这些应用程序设置它,如下所示:
public class MyConfiguration : DbConfiguration
{
public ReporsitoryConfiguration()
{
// your code here
}
}
Run Code Online (Sandbox Code Playgroud)
进而:
DbConfiguration.SetConfiguration(new MyConfiguration());
Run Code Online (Sandbox Code Playgroud)