gpt4 book ai didi

NHibernate 3.2 多对多代码映射

转载 作者:行者123 更新时间:2023-12-04 02:38:54 26 4
gpt4 key购买 nike

我正在努力学习 NHibernate 3.2 built-in mapping by code api ( 不是 FluentNHibernate,也不是 xml)。你能帮我映射这些实体之间的多对多关系吗?

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

我的主要关键策略是:
Id( 
t => t.Id,
t => {
t.Generator(Generators.HighLow, g => g.Params(new { max_low = 100 }));
t.Column(typeof(TEntity).Name + "Id");
});

我试试这个:
// 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);
}));

但它不起作用。

最佳答案

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

关于NHibernate 3.2 多对多代码映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9573326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com