c# – EF6’ModelConfiguration’设置但未被发现
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – EF6’ModelConfiguration’设置但未被发现,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4071字,纯文字阅读大概需要6分钟。
内容图文
![c# – EF6’ModelConfiguration’设置但未被发现](/upload/InfoBanner/zyjiaocheng/793/96ccebbe84bc4cf1b6cf54274076844c.jpg)
我有以下库:
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或Entity Framework.
这是来自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( .... )
}
}
}
从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.
在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
有人看过这个问题吗?
UPDATE
如上所述,我的解决方案中有几个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.
换句话说,环境似乎已经从测试期间使用的第一个DAL dll加载了ModelConfiguration,然后期望为它使用的后续DAL dll找到相同的ModelConfiguration.
这是否意味着我们在整个解决方案中只能有一个ModelConfiguration类?
解决方法:
我有相同的问题.
似乎与这里的解释相关的答案:
http://msdn.microsoft.com/en-us/data/jj680699
在某些情况下,无法将DbConfiguration类放在与DbContext类相同的程序集中.例如,您可能在不同的程序集中有两个DbContext类.处理此问题有两种选择.
第一个选项是使用配置文件指定要使用的DbConfiguration实例.为此,请设置entityFramework部分的codeConfigurationType属性.例如:
…你的EF配置……
codeConfigurationType的值必须是DbConfiguration类的程序集和名称空间限定名称.
第二个选项是将DbConfigurationTypeAttribute放在上下文类中.例如:
[DbConfigurationType(typeof(MyDbConfiguration))]
public class MyContextContext : DbContext
{
}
传递给属性的值可以是DbConfiguration类型(如上所示),也可以是程序集和名称空间限定类型名称字符串.例如:
[DbConfigurationType("MyNamespace.MyDbConfiguration, MyAssembly")]
public class MyContextContext : DbContext
{
}
我仍然使用DbConfigurationTypeAttribute获得该错误
内容总结
以上是互联网集市为您收集整理的c# – EF6’ModelConfiguration’设置但未被发现全部内容,希望文章能够帮你解决c# – EF6’ModelConfiguration’设置但未被发现所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。