在Entity Framework 4中使用POCO实体的每层次继承表 [英] Table per hierarchy inheritance with POCO entities in Entity Framework 4
问题描述
付款(基础类[应该是抽象的,但也有麻烦])
CreditCard付款具体实现)
ACHPayment(具体实现)
CheckPayment(具体实现)
现在,我只映射基类属性。所有这些类都在同一个命名空间中。他们有一个在数据库中称为PaymentTypeId的鉴别器,因此付款映射的条件为When PaymentTypeId = 0。每个子类具有与不同值相同的条件(即CreditCardPayment = 1等)。
当我尝试使用DataContext加载所有付款的每个列表。 Payments.ToList()(DataContext从ObjectContext继承)我得到以下异常:
无法找到具有标识的类型的对象映射DataLayer.DataModel.CreditCardPayment 。我不知道这是什么意思,因为POCO CreditCardPayment类与POCO Payment类在同一个命名空间中存在(实际上在同一个文件中)。
我缺少什么?
我以前没有代表(我没有认为它相关,但是是)。 CreditCardPayment是否继承自名为CreditPayment的中介类,ACHPayment继承自CashPayment。 CreditCardPayment和CashPayment在同一个命名空间和文件中存在,但EF映射中没有表示。一旦我在映射文件中添加了这些,一切都可以正常工作。
所以,即使EF没有直接映射到这些类型之一,似乎需要知道它们,因为它改变了CreditCardPayment类别的基本类型人。感谢您的帮助。
Our organization is looking to standardize on Entity Framework once v4 comes out. As a result, I am looking at what it would take to migrate our application that uses NHibernate for persistence to EF4 using POCO support. In a couple of places we use single table inheritance (also known as Table Per Hierarchy). I have been unable to get it to work using the following.
Payment (base class [should be abstract but having trouble there as well]) CreditCardPayment (concrete implementation) ACHPayment (concrete implementation) CheckPayment (concrete implementation)
Right now, I am mapping them with only the base class properties. All of these classes are in the same namespace. They have a discrimimator that is called PaymentTypeId in the database, so the Payment mapping has a condition of "When PaymentTypeId = 0". Each of the subclasses have the same condition with different values (i.e. CreditCardPayment = 1, etc.).
When I try to load each a list of all payments using DataContext.Payments.ToList() (DataContext inherits from ObjectContext) I am getting the following exception:
"Object mapping could not be found for Type with identity 'DataLayer.DataModel.CreditCardPayment'."
I can't figure out what this means, as the POCO CreditCardPayment class lives in the same namespace as the POCO Payment class does (in fact in the same file).
What am I missing?
What I didn't represent before (I didn't think it relevant, but it was). Was that CreditCardPayment inherited from an intermediary class named "CreditPayment" and ACHPayment inherited from CashPayment. CreditCardPayment and CashPayment live in the same namespace and file, but were not represented in the EF mapping. Once I added those within the mapping file, everything worked ok.
So, even thought EF does not ever map to one of those types directly, it seems to need to know about them, because it changes the basetype of the CreditCardPayment classes et al. Thank you for your help on this.
这篇关于在Entity Framework 4中使用POCO实体的每层次继承表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!