0
我有一個問題,似乎無法弄清楚我做錯了什麼。我有一個擁有多個組的公司,一個組有許多用戶,而一個用戶屬於一個組。向公司添加一個組可以正常工作,但是我無法將用戶添加到組中。流利NHibernate雙向HasMany映射離開孤立記錄
這裏是我的映射:
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();
}
在數據庫中,我有FK設置爲NOT NULL。密鑰位於名爲Group_Id的用戶表上。我正在使用以下方法將我的用戶分配到該組:
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 {});
}
我沒有收到任何錯誤,但Group_Id從未設置。任何幫助,將不勝感激!
謝謝,喬