今天我有一个有趣的问题!基本上我有两个班。继承流畅nhibenate映射问题
public class A : B
{
public virtual new ISet<DifferentItem> Items {get;set;}
}
public class B
{
public virtual int Id {get;set;}
public virtual ISet<Item> Items {get;set;}
}
子类A隐藏基类B属性Items,并用一个具有相同名称和不同类型的新属性替换它。
这些类的映射关系
public class AMapping : SubclassMap<A>
{
public AMapping()
{
HasMany(x=>x.Items)
.LazyLoad()
.AsSet();
}
}
public class BMapping : ClassMap<B>
{
public BMapping()
{
Id(x=>x.Id);
HasMany(x=>x.Items)
.LazyLoad()
.AsSet();
}
}
然而,当我运行我的单元测试,以检查映射我得到以下异常:
测试的映射:NHibernate.PropertyAccessException:无效的转换(检查你的映射属性类型不匹配); A ----> System.InvalidCastException的setter:无法强制类型'NHibernate.Collection.Generic.PersistentGenericSet 1[Item]' to type 'Iesi.Collections.Generic.ISet
1 [DifferentItem]'强制转换对象。
任何人有任何想法?
很明显,这与子类的集合类型有关。但是我通过映射类的可用选项浏览了一下,没有什么比这里更好的解决方案了。
我正在使用PersistenceSpecification来检查映射。 – 2010-06-02 11:33:20
在深入阅读Skeets C#的p103之后,看起来这可能与“缺乏协方差和相反性”有关。 – 2010-06-02 11:48:11