C# Nhibernate映射、级联、反转、更新、插入?
我有一个EmployeeMonth对象,用于保存员工计算的奖金和积分。在EmployeeMonth对象中有一个BonusMonth对象。BonusMonth对象设置员工每月必须达到的时间间隔以赚取奖金(此对象每月设置,不应更改) 当我重新计算奖金时减少了88%,我调整BonusMonth对象间隔,然后重新计算奖金。 然后,新奖金(结果)保存在同一EmployeeMonth对象中 然后,当我保存新的EmployeeMonth时,BonusMonth也会被保存 如何配置nhibernate映射,以便不保存BonusMonth?我只想更新/保存重新计算的EmployeeMonth值。 这就是我的映射的样子: EmployeeMonth.csC# Nhibernate映射、级联、反转、更新、插入?,c#,nhibernate,nhibernate-mapping,nhibernate-cascade,C#,Nhibernate,Nhibernate Mapping,Nhibernate Cascade,我有一个EmployeeMonth对象,用于保存员工计算的奖金和积分。在EmployeeMonth对象中有一个BonusMonth对象。BonusMonth对象设置员工每月必须达到的时间间隔以赚取奖金(此对象每月设置,不应更改) 当我重新计算奖金时减少了88%,我调整BonusMonth对象间隔,然后重新计算奖金。 然后,新奖金(结果)保存在同一EmployeeMonth对象中 然后,当我保存新的EmployeeMonth时,BonusMonth也会被保存 如何配置nhibernate映射,以便
public class EmployeeMonth
{
public virtual Guid Id { get; private set; }
public virtual BonusMonth BonusMonth { get; set; }
public virtual Employee Employee { get; set; }
public virtual int WorkReductionPercent { get; set; }
public virtual int Points { get; set; }
public virtual decimal Bonus{ get; set; }
}
BonusMonth.cs
public class BonusMonth
{
public virtual int BasicPoints { get; set; }
public virtual int ExtraPoints { get; set; }
public virtual int MaxPoints { get; set; }
public virtual decimal BasicBonus { get; set; }
public virtual decimal ExtraBonus { get; set; }
public virtual decimal MaxBonus { get; set; }
}
EmployeeMonth.hbm.xml
<class name="EmployeeMonth">
<id name="Id">
<generator class="guid" />
</id>
<property name="WorkReductionPercent"/>
<property name="Points"/>
<property name="Bonus"/>
<many-to-one name="Employee" unique-key="EpId" column="EmployeeId" cascade="none" not-null="true" insert="true" update="false" />
<many-to-one name="BonusMonth" column="BonusMonthId" cascade="none" not-null="true" insert="true" update="false" />
</class>
<class name="BonusMonth">
<id name="Id">
<generator class="guid" />
</id>
<property name="BasicPoints"/>
<property name="ExtraPoints"/>
<property name="MaxPoints"/>
<property name="BasicBonus"/>
<property name="ExtraBonus"/>
<property name="MaxBonus"/>
</class>
BonusMonth.hbm.xml
<class name="EmployeeMonth">
<id name="Id">
<generator class="guid" />
</id>
<property name="WorkReductionPercent"/>
<property name="Points"/>
<property name="Bonus"/>
<many-to-one name="Employee" unique-key="EpId" column="EmployeeId" cascade="none" not-null="true" insert="true" update="false" />
<many-to-one name="BonusMonth" column="BonusMonthId" cascade="none" not-null="true" insert="true" update="false" />
</class>
<class name="BonusMonth">
<id name="Id">
<generator class="guid" />
</id>
<property name="BasicPoints"/>
<property name="ExtraPoints"/>
<property name="MaxPoints"/>
<property name="BasicBonus"/>
<property name="ExtraBonus"/>
<property name="MaxBonus"/>
</class>
这与级联无关,因为BonusMonth实体与NHibernate会话相关联。我个人建议不要更改BonusMonth实体,除非您希望将更改保留到数据库中,但是如果您坚持,可以通过从会话中分离BonusMonth实体来解决此问题:
session.Evict(employeeMonth.BonusMonth)
您仍然需要使用cascade=“none”,否则在保存employeeMonth时,它只会重新附加实体