1

我在网上搜索了很多,但没有找到适合我的问题的正确解决方案。

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

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

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)

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

4

1 回答 1

1

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

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?

于 2012-05-09T04:38:55.403 回答