C# Fluent NHibernate 多对多
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/108396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fluent NHibernate Many-to-Many
提问by Ryan Lanciaux
I am using Fluent NHibernate and having some issues getting a many to many relationship setup with one of my classes. It's probably a stupid mistake but I've been stuck for a little bit trying to get it working. Anyways, I have a couple classes that have Many-Many relationships.
我正在使用 Fluent NHibernate 并且在与我的一个课程建立多对多关系时遇到了一些问题。这可能是一个愚蠢的错误,但我一直在努力让它发挥作用。无论如何,我有几个具有多对多关系的类。
public class Person
{
public Person()
{
GroupsOwned = new List<Groups>();
}
public virtual IList<Groups> GroupsOwned { get; set; }
}
public class Groups
{
public Groups()
{
Admins= new List<Person>();
}
public virtual IList<Person> Admins{ get; set; }
}
With the mapping looking like this
映射看起来像这样
Person: ...
人: ...
HasManyToMany<Groups>(x => x.GroupsOwned)
.WithTableName("GroupAdministrators")
.WithParentKeyColumn("PersonID")
.WithChildKeyColumn("GroupID")
.Cascade.SaveUpdate();
Groups: ...
团体:...
HasManyToMany<Person>(x => x.Admins)
.WithTableName("GroupAdministrators")
.WithParentKeyColumn("GroupID")
.WithChildKeyColumn("PersonID")
.Cascade.SaveUpdate();
When I run my integration test, basically I'm creating a new person and group. Adding the Group to the Person.GroupsOwned. If I get the Person Object back from the repository, the GroupsOwned is equal to the initial group, however, when I get the group back if I check count on Group.Admins, the count is 0. The Join table has the GroupID and the PersonID saved in it.
当我运行我的集成测试时,基本上我是在创建一个新的人和组。将组添加到 Person.GroupsOwned。如果我从存储库中取回 Person 对象,GroupsOwned 等于初始组,但是,如果我检查 Group.Admins 上的计数,当我取回组时,计数为 0。Join 表具有 GroupID 和PersonID 保存在其中。
Thanks for any advice you may have.
感谢您的任何建议。
采纳答案by Santiago Palladino
The fact that it is adding two records to the table looks like you are missing an inverse attribute. Since both the person and the group are being changed, NHibernate is persisting the relation twice (once for each object). The inverse attribute is specifically for avoiding this.
它向表中添加两条记录这一事实看起来就像您缺少一个反向属性。由于人和组都被更改,NHibernate 将关系持久化两次(每个对象一次)。inverse 属性专门用于避免这种情况。
I'm not sure about how to add it in mapping in code, but the link shows how to do it in XML.
我不确定如何将它添加到代码映射中,但链接显示了如何在 XML 中执行此操作。
回答by emeryc
Are you making sure to add the Person to the Groups.Admin? You have to make both links.
您确定将 Person 添加到 Groups.Admin 吗?你必须建立两个链接。
回答by emeryc
You have three tables right?
你有三张桌子吧?
People, Groups, and GroupAdministrators
人员、组和组管理员
when you add to both sides you get
当你添加到两边时,你会得到
People (with an id of p1) Groups (with an id of g1)
人(id 为 p1) 组(id 为 g1)
and in GroupAdministrators you have two columns and a table that has
在 GroupAdministrators 中,您有两列和一个表
(p1,g1)
(p1,g1)
(p1,g1)
(p1,g1)
and your unit test code looks like the following.
您的单元测试代码如下所示。
Context hibContext //Built here
Transaction hibTrans //build and start the transaction.
Person p1 = new Person()
Groups g1 = new Groups()
p1.getGroupsOwned().add(g1)
g1.getAdmins().add(p1)
hibTrans.commit();
hibContext.close();
And then in your test you make a new context, and test to see what's in the context, and you get back the right thing, but your tables are all mucked up?
然后在你的测试中你创建一个新的上下文,并测试看看上下文中有什么,然后你得到正确的东西,但是你的表都被搞砸了?
回答by emeryc
@Santiago I think you're right.
@圣地亚哥我认为你是对的。
The answer might just be that you need to remove one of your ManyToMany declarations, looking more at Fluent it looks like it might be smart enough to just do it for you.
答案可能只是你需要删除你的 ManyToMany 声明之一,更多地看 Fluent,它看起来可能足够聪明,可以为你做这件事。