FNH与属性的多对多关系

时间:2011-08-28 16:25:50

标签: c# asp.net fluent-nhibernate

我有三张表格如下:

内容:

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");
    }
}

这三个表同时我想创建一种相互关联的方式。 我想创建一个ContentFieldItem,关系Content通过contentid和ContentField通过fieldid和一些更多的参数。我认为这可以通过多对多没有参数完成,但我必须使用参数,这怎么可能与流利的nhibernate?

现在得到“无法确定类型:Project.Models.ContentField”。

1 个答案:

答案 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.澄清你的意思吗?

希望这有帮助