I'm trying to learn NHibernate 3.2 built-in mapping by code api
(NOT FluentNHibernate, nor xml). Can you help me to map a many-to-many relationship between these entities please?
public class Post {
public virtual Id { get; set; }
public IList<Tag> Tags { get; set; }
}
public class Tag {
public virtual Id { get; set; }
public IList<Post> Posts { get; set; }
}
My primary key strategy is:
Id(
t => t.Id,
t => {
t.Generator(Generators.HighLow, g => g.Params(new { max_low = 100 }));
t.Column(typeof(TEntity).Name + "Id");
});
and I try this:
// TagMap : ClassMapping<Tag>
Bag(t => t.Posts, bag => {
bag.Inverse(true);
bag.Table("TagsPosts");
bag.Cascade(Cascade.DeleteOrphans);
}, t => t.ManyToMany(c => {
c.Column("PostId");
c.Lazy(LazyRelation.Proxy);
}));
// PostMap : ClassMapping<Post>
Bag(t => t.Tags, bag => {
bag.Table("TagsPosts");
bag.Cascade(Cascade.DeleteOrphans);
}, t => t.ManyToMany(c => {
c.Column("TagId");
c.Lazy(LazyRelation.Proxy);
}));
but it doesn't work.
// Post Map
Bag(x => x.Tags, collectionMapping =>
{
collectionMapping.Table("TagPosts");
collectionMapping.Cascade(Cascade.None);
collectionMapping.Key(k => k.Column("PostID"));
},
map => map.ManyToMany(p => p.Column("TagID")));
// Tag Map
Bag(x => x.Posts, collectionMapping =>
{
collectionMapping.Table("TagPosts");
collectionMapping.Cascade(Cascade.None);
collectionMapping.Key(k => k.Column("TagID"));
},
map => map.ManyToMany(p => p.Column("PostID")));