[英]Fluent NHibernate Bidirectional HasMany Mapping leaving orphaned records
I am having an issue and cant seem to figure out what I am doing wrong. 我遇到问题,似乎无法弄清楚我在做什么。 I have a company that has many groups, a group has many users, and a user belongs to a group.
我的公司有很多组,一个组有很多用户,而一个用户属于一个组。 Adding a group to a company works fine, however I am not able to add a user to a group.
将组添加到公司工作正常,但是我无法将用户添加到组。
here is my mapping: 这是我的映射:
public GroupMapping()
{
Id(x => x.Id);
Map(x => x.Name);
References(x => x.Company).Column("Company_Id");
HasMany(x => x.Users)
.Table("User")
.KeyColumn("Group_Id")
.Cascade.None();
}
public UserMapping()
{
Id(x => x.Id);
References(x => x.Group).Column("Group_Id");
HasMany(x => x.Role)
.KeyColumn("User_Id")
.Cascade.All();
}
On the database I have the FK set to Not Null. 在数据库上,我将FK设置为Not Null。 The Key is on the User Table called Group_Id.
密钥在用户表上,称为Group_Id。 I am assigning my user to the group using the following method:
我正在使用以下方法将用户分配给该组:
public JsonResult AssignGroup(int id, int groupId)
{
var user = UserRepository.GetById(id);
var group = GroupRepository.GetById(groupId);
user.Group = group;
UserRepository.Save(user);
return Json(new {});
}
I do not receive any errors, however the Group_Id is never set. 我没有收到任何错误,但是从未设置Group_Id。 Any help would be appreciated!
任何帮助,将不胜感激!
Thanks, Joe 谢谢乔
HasMany(x => x.Users)
is missing an Inverse()
otherwise it throws when you add a user. HasMany(x => x.Users)
缺少Inverse()
否则在添加用户时会抛出该异常。 Only the User should maintain the FK session.Flush()
or transaction.Commit();
session.Flush()
或transaction.Commit();
in your repository method
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.