Fluent NHibernate插入父id问题与HasMany关系
我不明白为什么 NHibernate 会在没有父实体外键的情况下插入子实体。 我发现解决这个问题的唯一方法是使用双向关系,还有其他方法吗?
以下是这些类:
public class Parent
{
public virtual int ParentId {get; private set;}
public virtual IList<Child> Notes {get; private set;}
}
public class Child
{
public virtual ChildId {get; private set;}
public virtual Name {get; private set;}
}
这是我的 Fluent NHibernate 映射
public class ParentClassMap : ClassMap<Parent>
{
public ParentClassMap(){
Id(x => x.ParentId);
HasMany(x => x.Notes).Cascade.AllDeleteOrphan();
}
}
public class ChildClassMap : ClassMap<Child>
{
public ChildClassMap() {
Id(x => x.ChildId);
Map(x => x.Name);
}
}
当我将一个子级添加到父级的子级集合并保存父级时,父级和子级将被插入到数据库中,但子级的插入没有父级的外键(它有一个空值)
这是生成的插入:
INSERT INTO Child(ChildId, Name)
但它应该是:
INSERT INTO Child(ChildId, Name, ParentId)
我想补充一点,我不想用双向关系来解决这个问题,我不希望子级拥有对父级的引用。谢谢!!
I can't understand why NHibernate is inserting a child entity without the foreign key of the parent.
The only way I found to solve this is with a Bidirectional relationship, is there another way?
Here are the classes:
public class Parent
{
public virtual int ParentId {get; private set;}
public virtual IList<Child> Notes {get; private set;}
}
public class Child
{
public virtual ChildId {get; private set;}
public virtual Name {get; private set;}
}
Here is my Fluent NHibernate mapping
public class ParentClassMap : ClassMap<Parent>
{
public ParentClassMap(){
Id(x => x.ParentId);
HasMany(x => x.Notes).Cascade.AllDeleteOrphan();
}
}
public class ChildClassMap : ClassMap<Child>
{
public ChildClassMap() {
Id(x => x.ChildId);
Map(x => x.Name);
}
}
When I add a child to the parent's child collection and save the parent, the parent and the child are inserted into the database, but the child is inserte withoutthe foreign key to the parent (it has a null value)
This is the insert that is generated:
INSERT INTO Child(ChildId, Name)
But it should be:
INSERT INTO Child(ChildId, Name, ParentId)
I want to add that i don't want to resolve this with a bidirectional relationship, i do not want the child to have a reference to the parent. Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
Not.KeyNullable()
添加到您的HasMany
映射中。注意:此功能需要 NHibernate 3.2。
Add
Not.KeyNullable()
to yourHasMany
mapping.Note: This feature requires NHibernate 3.2.
您的父类映射应该有一个其子类的逆映射,
谢谢
尼勒什
Your parent class mapping should have an inverse for its child
Thanks
Neelesh