开发者

FNH Many-to-Many relationship with attributes

I have three tables as follow:

Content:

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 in开发者_开发问答t 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");
    }
}

These three tables at the same time I want to create a way of inter-related. I want to create a ContentFieldItem with relation Content through contentid and ContentField through fieldid and some more parameters.I think this can be done with many-to-many without parameters but i have to do with parameters, how is this possible with fluent nhibernate?

and now m getting "Could not determine type for: Project.Models.ContentField".


i would do

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

can you clarify what you mean with ContentField through fieldid and some more parameters.?

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜