继承的流畅的 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();
}
}
但是,当我运行单元测试来检查映射时,出现以下异常:
测试 A 映射:NHibernate.PropertyAccessException :无效的转换(检查您的映射是否有属性类型不匹配); A 的二传手 ----> System.InvalidCastException:无法将类型“NHibernate.Collection.Generic.PersistentGenericSet1[Item]”的对象强制转换为类型“Iesi.Collections.Generic.ISet
1[DifferentItem]”。
有人有什么想法吗?
显然,这与子类上的集合类型有关。但我浏览了映射类上的可用选项,没有找到任何突出的解决方案。
I have an interesting issue today!! Basically I have two classes.
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;}
}
The subclass A hides the base class B property, Items and replaces it with a new property with the same name and a different type.
The mappings for these classes are
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();
}
}
However when I run my unit test to check the mapping I get the following exception:
Tests the A mapping: NHibernate.PropertyAccessException : Invalid Cast (check your mapping for property type mismatches); setter of A
----> System.InvalidCastException : Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericSet1[Item]' to type 'Iesi.Collections.Generic.ISet
1[DifferentItem]'.
Anyone have any ideas?
Clearly it is something to do with the type of the collection on the sub-class. But I skimmed through the available options on the mapping class and nothing stood out as being the solution here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
C# 中的泛型不支持协变,因此本质上您不能使用
ISet
和ISet
。由于这是语言的限制,您需要重新考虑您的设计。或者等到 c# 6。Generics in c# does not support covariance, so essentially you can't have
ISet<Item>
andISet<DifferentItem>
. Since it's a limitation of the language you need to rethink your design. Or wait til c# 6.