NHibernate 不更新子实体
当尝试更新父类中的子项时,我遇到问题
public class Parent
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Child> MyChildren { get; set; }
}
public class Child
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Parent MyParent { get; set; }
}
我所做的就是获取 MyChildren 列表中的一项并修改它,但是当从会话调用 SaveOrUpdate 方法时,MyClildren 不会被保存。
注意:如果父级中的其他字段被修改,它会在数据库中更新。
更新:
<class name="Parent" table="Parent" lazy="true">
<id name="id" column="ID" type="int">
<generator class="identity" />
</id>
<bag cascade="all" lazy="true" name="MyChildren">
<key column="ID"/>
<one-to-many class="SND.Domain.Model.Child" />
</bag>
</class>
<class name="Child" table="Child" lazy="true">
<id name="id" column="ID" type="int">
<generator class="identity" />
</id>
<!-- Here i have another reference -->
<many-to-one name="AnotherEntity" class="SND.Domain.Model.AnotherEntity" column="entity_ID"/>
<property name="Name" column="Name" type="string" not-null="false" />
</class>
谢谢, 佩德罗
I am facing a problem when trying to update a child item in Parent class
public class Parent
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Child> MyChildren { get; set; }
}
public class Child
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Parent MyParent { get; set; }
}
What i do is to take one item of the MyChildren list and modify it, but when calling SaveOrUpdate Method from Session MyClildren is not being saved.
Note: if other fields in parent are modified it get updated in DB.
Update:
<class name="Parent" table="Parent" lazy="true">
<id name="id" column="ID" type="int">
<generator class="identity" />
</id>
<bag cascade="all" lazy="true" name="MyChildren">
<key column="ID"/>
<one-to-many class="SND.Domain.Model.Child" />
</bag>
</class>
<class name="Child" table="Child" lazy="true">
<id name="id" column="ID" type="int">
<generator class="identity" />
</id>
<!-- Here i have another reference -->
<many-to-one name="AnotherEntity" class="SND.Domain.Model.AnotherEntity" column="entity_ID"/>
<property name="Name" column="Name" type="string" not-null="false" />
</class>
Thanks,
Pedro
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看不到您的映射文件,但是您的子集合在父映射文件中设置了级联属性吗?
I can't see your mapping file, but do you have the cascade attribute on your child collection set in the parent mapping file?
我认为这是一个映射问题,试试这个:
Update1
It's a mapping trouble I think, try this :
Update1