[英]Fluent NHibernate Bidirectional HasMany Mapping leaving orphaned records
我遇到問題,似乎無法弄清楚我在做什么。 我的公司有很多組,一個組有很多用戶,而一個用戶屬於一個組。 將組添加到公司工作正常,但是我無法將用戶添加到組。
這是我的映射:
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。 任何幫助,將不勝感激!
謝謝喬
HasMany(x => x.Users)
缺少Inverse()
否則在添加用戶時會拋出該異常。 只有用戶應維護FK session.Flush()
或transaction.Commit();
在您的存儲庫方法中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.