使用接口映射多对多

时间:2012-05-08 23:38:19

标签: c# nhibernate interface many-to-many nhibernate-mapping

我搜索了很多网页,但找不到适合我问题的解决方案。

基本上我正在尝试与属性创建多对多关系,但正如标题所说它应该使用接口而不是普通类。

这是我在关系中的两个接口:

public interface IFaq  
{  
     IList<IFaqTagReference> FaqTagReferences { get; set; }  
     ...
}

public interface ITag
{  
     IList<IFaqTagReference> FaqTagReference { get; set; }
     ...
}  

和第三个指定关系的接口

public interface IFaqTagReference  
{  
     IFaq Faq { get; set; }  
     ITag Tag { get; set; }  
}

在nhibernate映射中我尝试这种方式(通常每个元素都有属性,但它只是不起作用的关系):

var mapper = new ConventionModelMapper();  
mapper.Class<Faq>(map =>  
                  {
                     map.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
                     map.Set(x => x.FaqTagReferences,
                             db =>
                             {
                                 db.Key(km =>
                                        {
                                            km.Column("FaqId");
                                            km.NotNullable(true);
                                        });                            
                                 db.Table("FaqTagReferences");
                                 db.Cascade(Cascade.None);
                                 db.Inverse(true);
                             }, 
                             r => r.OneToMany(m => m.Class(typeof(FaqTagReference)))    
                             );
                   });

mapper.Class<Tag>(map =>
                 {   
                     map.Id(x => x.Id, m => m.Generator(Generators.GuidComb));                 
                     map.Set(x => x.FaqTagReferences,
                             db =>
                             {
                                  db.Key(km =>
                                         {
                                              km.Column("TagId");
                                              km.NotNullable(true);
                                         });
                                  db.Table("FaqTagReferences");
                                  db.Cascade(Cascade.None);
                                  db.Inverse(true);
                             }, 
                             r => r.OneToMany(m => { m.Class(typeof(FaqTagReference)); })
                             );
                 });

mapper.Class<FaqTagReference>(map =>
                         {
                            map.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
                            map.ManyToOne(x => x.Faq, db => { db.Column("FaqId"); db.Class(typeof(Faq)); });
                            map.ManyToOne(x => x.Tag, db => { db.Column("TagId"); db.Class(typeof(Tag)); });
                         });

但是这会抛出异常

Could not determine type for:
System.Collections.Generic.IList[[Entities.IFaqTagReference,
BaseInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]],
mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, for
columns: NHibernate.Mapping.Column(FaqTagReferences)

感谢每一个提示或解决方案

1 个答案:

答案 0 :(得分:1)

我认为可能与liskov替代原则有关

http://en.wikipedia.org/wiki/Liskov_substitution_principle

essentially type IList<IFaqTagReference> is not type IList<FaqTagReference>
类似于 How do you map an entity -> interface relationship using Fluent NHibernate?