实体框架代码优先和多个程序集 [英] Entity Framework Code First and Multiple Assemblies
问题描述
我有一个与它的基类不同的程序集中的子类。父级是用于EF Code First的POCO类。
I have a subclass in a different assembly to its base class. The parent is a POCO class used for EF Code First.
当我尝试将继承的类的实例添加到数据库时,出现 InvalidOperationException: 找不到标识为'Foo.Bar.MyInheritedClass'的Type的对象映射。
When I try to add an instance of inherited class to the database I get InvalidOperationException: "Object mapping could not be found for Type with identity 'Foo.Bar.MyInheritedClass'".
如果子类与基类在同一程序集中,则效果很好。
It works fine if subclass is in same assembly as base class.
在常规EF中,解决方案似乎是对 ObjectContext.MetadataWorkspace.LoadFromAssembly(assembly)的调用。但是我不知道这与Code First有什么关系。
In regular EF the solution seems to be a call to ObjectContext.MetadataWorkspace.LoadFromAssembly(assembly). But I can't figure out how this relates to Code First.
有什么建议吗?
我正在使用 Entity Framework 4.1 RC。
推荐答案
我解决了这个问题,方法是从第一个程序集的DbContext继承,为派生类添加一个DbSet<>,然后添加新实例
I solved this by inheriting from the first assembliy's DbContext, adding a DbSet<> for the derived class, and then adding new instances of derived type to to that.
在MSDN论坛上发布的代码此处。
Posted code on MSDN forum here.
这篇关于实体框架代码优先和多个程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!