2010-06-02 60 views
1

今天我有一個有趣的問題!基本上我有兩個班。繼承流暢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]'強制轉換對象。

任何人有任何想法?

很明顯,這與子類的集合類型有關。但是我通過映射類的可用選項瀏覽了一下,沒有什麼比這裏更好的解決方案了。

+0

我正在使用PersistenceSpecification來檢查映射。 – 2010-06-02 11:33:20

+0

在深入閱讀Skeets C#的p103之後,看起來這可能與「缺乏協方差和相反性」有關。 – 2010-06-02 11:48:11

回答

1

c#中的泛型不支持協方差,所以基本上你不能有ISet<Item>ISet<DifferentItem>。由於這是語言的限制,您需要重新考慮設計。或者等到c#6.