[英]fluent NHibernate not saving the foreign key in HasMany. The column remains null
我正在努力在我的映射中使用外键。
我的 Model 看起来像这样:
public class Accountant: Entity
{
public virtual string Name { get; set; }
public virtual IList<Company> Companies { get; set; }
}
public class Company: Entity
{
public virtual string Name { get; set; }
public virtual Subscriber Subscriber { get; set; }
public virtual Accountant Accountant { get; set; }
public virtual Funder Funder { get; set; }
}
我的映射看起来像这样
public class AccountantMap : ClassMap<Accountant>
{
public AccountantMap()
{
Id(x => x.Id);
Map(x => x.Name);
HasMany(x => x.Companies)
.Inverse()
.Cascade.All();
Table("tblAccountant");
}
}
public class CompanyMap : ClassMap<Company>
{
public CompanyMap()
{
Id(x => x.Id);
Map(x => x.Name);
References(x => x.Subscriber).Cascade.SaveUpdate();
References(x => x.Accountant).Cascade.SaveUpdate();
References(x => x.Funder).Cascade.SaveUpdate();
Table("tblCompany");
}
}
因此,我正在尝试做的是,我正在尝试保存会计 Object 并且它必须更新表 tblCompany 中的外键
这是我的保存方法的样子
public void Create_Accountant()
{
var repo = new Repository();
var companies = new List<Company>();
companies.Add(repo.GetById<Company>(new Guid("02032BD9-2769-4183-9750-AF1F00A5E191")));
companies.Add(repo.GetById<Company>(new Guid("F86E8B40-73D2-447E-A525-AF1F00A5E191")));
var accountant = new Accountant
{
Name = "Accountant Simba",
Companies= companies
};
repo.Save(accountant);
}
public void Save<T>(T entity) where T: Entity
{
using (var session = _factory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
try
{
session.SaveOrUpdate(entity);
session.Flush();
transaction.Commit();
//return entity.Id;
}
catch (Exception)
{
transaction.Rollback();
throw;
}
}
}
}
代码执行后,这就是我的数据库中的内容
您会注意到 Account_id 列是空的,它不应为空。
有人,请帮助我,我做错了什么?
引用未设置,但Inverse()
告诉 NHibernate 使用引用。 将代码更改为此,然后您就不会忘记设置它了。
public class Accountant : Entity
{
public Accountant()
{
Companies = new ParentAwareCollection<Company>(c => c.Accountant = this, c => c.Accountant = null);
}
public virtual string Name { get; set; }
public virtual IList<Company> Companies { get; protected set; }
}
public sealed class ParentAwareCollection<TChild> : Collection<TChild>
{
private readonly Action<TChild> _setParent;
private readonly Action<TChild> _removeParent;
public ParentAwareCollection(Action<TChild> setParent, Action<TChild> removeParent)
{
_setParent = setParent;
_removeParent = removeParent;
}
protected override void InsertItem(int index, TChild item)
{
base.InsertItem(index, item);
_setParent(item);
}
protected override void RemoveItem(int index)
{
TChild removedItem = this[index];
_removeParent(removedItem);
base.RemoveItem(index);
}
protected override void ClearItems()
{
foreach (var item in this)
{
_removeParent(item);
}
base.ClearItems();
}
}
和用法
var accountant = new Accountant
{
Name = "Accountant Simba",
Companies =
{
repo.GetById<Company>(new Guid("02032BD9-2769-4183-9750-AF1F00A5E191")),
repo.GetById<Company>(new Guid("F86E8B40-73D2-447E-A525-AF1F00A5E191"))
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.