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. The Key is on the User Table called 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. Any help would be appreciated!
Thanks, Joe
HasMany(x => x.Users)
is missing an Inverse()
otherwise it throws when you add a user. Only the User should maintain the FK session.Flush()
or transaction.Commit();
in your repository method
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.