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(); } }
}
精彩评论