流利NHibernate的HasMany收集问题 [英] Fluent NHibernate HasMany Collection Problems
问题描述
看来,将我的映射从Cascade.All()更改为Cascade.AllDeleteOrphan()可以修复大部分问题。我仍然必须在OperatingState上明确地设置公司属性,这似乎是不必要的,因为它被添加到公司实体中,但至少我可以在更新期间使用它。我仍然需要用创建来测试。
如果有任何人可以解释,那将是一个很大的帮助。
更新2: 在玩了一些之后,看起来我并不总是必须指定父实体。
原始发布
我有2个相关的实体
pre $ public class Company {
// ...字段
公共虚拟IList< OperatingState> OperatingStates {get;组; }
}
public class OperatingState {
public virtual Company Company {get;组; } //映射到CompanyID
public virtual string State {get;组; }
}
它们映射如下:
public class CompanyMap:ClassMap< Company> {
public CompanyMap(){
// ... fields
HasMany(x => x.OperatingStates)
.Cascade.All()
.Table ( OperatingState);
}
}
public class OperatingStateMap:ClassMap< OperatingState> {
public OperatingStateStateMap(){
Id(x => x.ID);
参考文献(x => x.Company);
Map(x => x.State);
$所以一切都好,直到我试图更新公司新营运状态 公司公司= _repo.GetSingle(123);
company.OperatingStates.Clear();
foreach(字符串状态在form [OperatingStates]。Split(',')){
company.OperatingStates.Add(new OperatingState(state));
}
_repo.Save(公司); //调用ISession.SaveOrUpdate
它爆炸了:
无法将值NULL插入
列'CompanyID',表
'ConsumerCartel.dbo.CompanyOperatingState';
列不允许有空值。 INSERT
失败。
然而,如果我做了2次修改,那么这样做是有效的。
Company company = _repo.GetSingle(123);
//这次不清除列表;
foreach(字符串状态在form [OperatingStates]。Split(',')){
OperatingState os = new OperatingState(state);
//明确设定公司
os.Company = company;
company.OperatingStates.Add(os);
}
_repo.Save(公司); //调用ISession.SaveOrUpdate
除了旧的,还会添加新的状态不是我想要的。但是,即使在明确地设置公司时(即将其添加到映射列表时,我不应该这么做),如果列表被清除,那么 。
$ b
这个代码在其他实体上工作,但不在这个实体上,所以我认为这个应该写成。我在做什么错了?
解决方案你试过使用Inverse()吗?
HasMany(x => x.OperatingStates)
.Inverse()
.Cascade.All()
.Table(OperatingState );
Update:
It appears that changing my mapping from Cascade.All() to Cascade.AllDeleteOrphan() fixes most of my issues. I still have to explicitly set the Company property on the OperatingState, which seems unnecessary as it's being added to the Company entity, but at least I can work with that during an update. I still need to test that with a create.
If any one can explain that, that would be a big help.
Update 2: After playing with it some more, it appears I don't always have to specify the parent entity.
Original Post
I have 2 related entities
public class Company {
//... fields
public virtual IList<OperatingState> OperatingStates { get; set; }
}
public class OperatingState {
public virtual Company Company { get; set; }// Mapped on CompanyID
public virtual string State { get; set; }
}
And they are mapped like this:
public class CompanyMap : ClassMap<Company> {
public CompanyMap() {
//... fields
HasMany(x => x.OperatingStates)
.Cascade.All()
.Table("OperatingState");
}
}
public class OperatingStateMap : ClassMap<OperatingState> {
public OperatingStateStateMap() {
Id(x => x.ID);
References(x => x.Company);
Map(x => x.State);
}
}
So all is well until I try to update Company with new Operating States
Company company = _repo.GetSingle(123);
company.OperatingStates.Clear();
foreach(string state in form["OperatingStates"].Split(',')) {
company.OperatingStates.Add(new OperatingState(state));
}
_repo.Save(company); // calls ISession.SaveOrUpdate
It bombs out with:
Cannot insert the value NULL into
column 'CompanyID', table
'ConsumerCartel.dbo.CompanyOperatingState';
column does not allow nulls. INSERT
fails. The statement has been
terminated.
However, if I make 2 changes it kind of works
Company company = _repo.GetSingle(123);
// don't clear the list this time;
foreach(string state in form["OperatingStates"].Split(',')) {
OperatingState os = new OperatingState(state);
// explicitly setting the company
os.Company = company;
company.OperatingStates.Add(os);
}
_repo.Save(company); // calls ISession.SaveOrUpdate
Which will add the new states in addition to the old ones, which is not what I want. However, even when explicitly setting the company (which I shouldn't have to do when it's added to a mapped list?) it doesn't work if the list is cleared.
This code has worked on other entities, but not on this one, so I think this should work as written. What am I doing wrong?
解决方案 Have you tried using Inverse()?
HasMany(x => x.OperatingStates)
.Inverse()
.Cascade.All()
.Table("OperatingState");
这篇关于流利NHibernate的HasMany收集问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!