2011-08-28 67 views
1

我有三個表如下關係:FNH多到許多帶有屬性

內容:

public class Content 
{ 
    public Content() 
    { 
     ContentFieldItems = new List<ContentFieldItem>(); 
     ContentFields = new Dictionary<ContentField, ContentFieldItem>(); 
     People = new List<Person>(); 
    } 

    public virtual int id { get; set; } 
    public virtual int categoryid { get; set; } 
    public virtual int userid { get; set; } 
    public virtual DateTime? date { get; set; } 
    public virtual int status { get; set; } 
    public virtual IDictionary<ContentField, ContentFieldItem> ContentFields { get; set; } 
    public virtual IList<ContentFieldItem> ContentFieldItems { get; set; } 
    public virtual IList<Person> People { get; set; } 
    public virtual string title { get; set; } 
} 

public class ContentMap : ClassMap<Content> 
{ 
     Id(x => x.id); 
     Map(x => x.categoryid); 
     Map(x => x.userid); 
     Map(x => x.date); 
     Map(x => x.status); 
     Map(x => x.title); 

     HasMany<ContentFieldItem>(x=>x.ContentFields) 
      .Table("ContentFieldItem") 
      .KeyColumn("contentid") 
      .AsMap(f => f.ContentField) 
      .Component(c => 
      { 
       c.ParentReference(m => m.Content); 
       c.References(m => m.ContentField); 
       c.Map(m => m.fieldadditionalinfo); 
       c.Map(m => m.fieldvalue); 
      }) 
      .Inverse() 
      .Cascade.AllDeleteOrphan(); 

      ... 
} 

ContentField:

public class ContentField 
{ 
    public ContentField() 
    { 
     Contents = new List<Content>(); 
    } 

    public virtual int id { get; set; } 
    public virtual string field { get; set; } 
    public virtual IList<Content> Contents { get; set; } 

    public virtual void AddContentField(Content content, 
     ContentFieldItem contentfielditem) 
    { 
     Contents.Add(content); 
     content.ContentFields.Add(this, contentfielditem); 
    } 
} 

public class ContentFieldMap : ClassMap<ContentField> 
{ 
    public ContentFieldMap() 
    { 
     Id(x => x.id); 
     Map(x => x.field); 
     Map(x => x.categoryid); 
     Map(x => x.status); 
     Map(x => x.type); 
     Map(x => x.showonall); 
    } 
} 

ContentFieldItem:

public class ContentFieldItem 
{ 
    public virtual int id { get; set; } 
    public virtual string fieldvalue { get; set; } 
    public virtual string fieldadditionalinfo { get; set; } 
    public virtual Content Content { get; set; } 
    public virtual ContentField ContentField { get; set; } 
} 

public class ContentFieldItemMap : ClassMap<ContentFieldItem> 
{ 
    public ContentFieldItemMap() 
    { 
     Id(x => x.id); 
     Map(x => x.fieldvalue); 
     Map(x => x.fieldadditionalinfo); 
     References(x => x.Content, "contentid"); 
     References(x => x.ContentField, "fieldid"); 
    } 
} 

sa上的這三張桌子我的時間我想創造一種相互關聯的方式。 我想創建一個與關係內容的ContentFieldItem通過contentid和ContentField通過fieldid和一些更多的參數。我認爲這可以用多參數來完成,而不需要參數,但我必須處理參數,這怎麼可能與流利的nhibernate ?

現在m得到「無法確定類型爲:Project.Models.ContentField」。

+0

'的hasMany (X => x.ContentFields)'應該是'的hasMany(X => x.ContentFields)'或'的hasMany (X => X .ContentFields)' – Firo

+0

什麼是IList ContentFieldItems'有用? 'ContentFields.Values'給你同樣的結果 – Firo

+0

在兩種方式下給出了相同的錯誤。 –

回答

0

我會做

public class Content 
{ 
    public virtual int Id { get; set; } 

    public virtual string Title { get; set; } 
    public virtual IDictionary<ContentField, ContentFieldInfo> Fields { get; set; } 
} 

public class ContentField 
{ 
    public virtual int Id { get; set; } 

    public virtual string Name { get; set; } 
} 

public class ContentFieldInfo 
{ 
    public virtual Content Content { get; set; } 
    public virtual ContentField Field { get; set; } 

    public virtual string Values { get; set; } 
    public virtual string AdditionalInfo { get; set; } 
} 

class ContentMap : ClassMap<Content> 
{ 
    public ContentMap() 
    { 
     Id(...); 

     Map(c => c.Title); 

     HasMany(c => c.Fields) 
      .Table("ContentFieldItem") 
      .KeyColumn("contentid") 
      .AsMap(fieldinfo => fieldinfo.Field) 
      .Component(comp => 
      { 
       comp.ParentReference(fi => fi.Content); 
       comp.References(fi => fi.ContentField); 
       comp.Map(fi => fi.Value); 
       ... 
      }); 
    } 
} 

class ContentFieldMap : ClassMap<ContentField> 
{ 
    public ContentFieldMap() 
    { 
     Id(...); 

     Map(c => c.Name); 
    } 
} 

你能澄清你的意思ContentField through fieldid and some more parameters.什麼?

希望這有助於

+0

它只是一個錯誤的句子。即時獲取「無法確定類型:Project.Models.ContentField」錯誤,並不能解決。 –

+0

@bakbeyimsanaikiciftlafimvar:您是否將ContentFieldMap添加到了sessionfactory? – Firo

+0

是的。我添加了以下內容:x.FluentMappings.AddFromAssemblyOf (); –