如何阻止 Fluent NHibernate 创建外键
我得到了如下例所示的多态关系:
public class A
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
}
Class B & C 包含 A 列表:
public class B/C
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<A> As { get; set; }
public virtual SomeParent Parent { get; set; }
}
我的目标是像“
session.Linq<B>().Where(x => x.Parent == someParent && x.As.Contains(someA));
当前我在 B => 之间配置了多对多关系”这样的查询。 A和C=> A 使用共享链接表,因为我希望将所有链接放在一个表中。 在此示例中,NH shema 导出创建 4 个表:A、B、C 和 ChildToA。
HasManyToMany(x => x.As)
.AsBag()
.Table("XX_ChildToA")
.ParentKeyColumn("Child_ID")
.ChildKeyColumn("A_ID")
.Cascade.All()
只要您只使用一个子类型,这种方法就可以正常工作,因为 shema 导出会生成一个外键,将“Child_ID”限制为导出时首先命中的任何表的 ID(在本例中为 B)。
var b = session.Get<B>(id);
b.As.Add(someA);
tx.Commit(); // works fine
var c = session.Get<C>(id);
c.As.Add(someA);
tx.Commit(); // crashes because of FK constraint
我可以阻止 FluentNHibernate 创建此 FK 吗?当我在 google 上搜索这个问题时,我注意到 HBM 样本在多对一关系中具有foreign-key="no" 属性。那么 NHibernate 应该能够解决这个问题,对吗?不过,我想保留我的流畅映射,因为我可以通过这种方式为所有子类型创建通用基映射类,并且目前我们所有的映射都是 FNH 映射。
I got a polymorphic relationship like the following example:
public class A
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
}
Class B & C contining a List of A's:
public class B/C
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<A> As { get; set; }
public virtual SomeParent Parent { get; set; }
}
My goal are queries like
session.Linq<B>().Where(x => x.Parent == someParent && x.As.Contains(someA));
Currently I configured a Many-To-Many relation between B => A and C => A using a shared link table because I want to have all my links in one table.
In this examle NH shema export creates 4 tables, A, B, C and ChildToA.
HasManyToMany(x => x.As)
.AsBag()
.Table("XX_ChildToA")
.ParentKeyColumn("Child_ID")
.ChildKeyColumn("A_ID")
.Cascade.All()
This works fine as long as you use only one the child types because shema export generates a foreign key restricting the "Child_ID" to IDs of whatever table it hits first while exporting (B in this case).
var b = session.Get<B>(id);
b.As.Add(someA);
tx.Commit(); // works fine
var c = session.Get<C>(id);
c.As.Add(someA);
tx.Commit(); // crashes because of FK constraint
Can I stop FluentNHibernate from creating this FK? While I searched google for this problem I noticed HBM samples with foreign-key="no" attributes in many-to-one relationships. So NHibernate should be able to solve this, right? However I would like to keep my fluent mappings because I can create a generic base mapping class for all my child types this way and currently all our mappings are FNH mappings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可以做到:
This should do it:
我并不完全熟悉 FluentNHibernate,但我假设您可以使用以下内容在集合的映射上设置此自定义属性:
.SetAttribute("foreign-key", "no")
I'm not entirely familiar with FluentNHibernate, but I'm assuming you could set this custom attribute on your mapping for the collection using something like:
.SetAttribute("foreign-key", "no")