开发者

NHibernate-Mapping question

I have a little mapping problem for NHibernate. First of all my Database structure looks like this.

A Table Post(Id,开发者_Python百科 Title, Text ...... ) with Articels A Table Tag(Tagname) with Tags (Tagcloud) A Table PostTag(PostId, TagName) for mapping Post and Tag.

So every post can have more Tags. Now I wanna map the Tags in a Post as a basic collection with strings, not as objects of Tag for example. So does anyone know how to do this? I'm new to this and I cant find a answer until now :)

So faithfully. Jan


when you dont need to update tag:

HasMany(x => x.Tags)
    .Table("PostTag")
    .KeyColumn("PostId")
    .Element("Tagname");

if you need to update then:

HasManyToMany(x => x.Tagnames)
    .Table("PostTag")
    .AsBag()  // or AsArray()
    .ParentKeyColumn("PostId")
    .ChildKeyColumn("TagName")
    .Element("TagName");

Or

class Post
{
    public virtual ISet<Tag> Tags { get; set; }

    public virtual string[] Tagnames
    { get { return Tags.Select(t => t.Name).ToArray(); } }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜