nHibernate,一对一或零关系困扰

Sea*_*eau 5 c# nhibernate nhibernate-mapping

我有两节课.一个被调用Employee,另一个EmployeeDetails与其父'Employee'类具有零或一个关系.换句话说,在某些情况下我们需要将额外的数据存储到此类中,'EmployeeDetails'但这不一定是常态.db结构非常简单,"EmployeeDetails"与其父级共享相同的ID.

我得到的问题是'EmployeeDetails''Employee'类中删除类,我会想象将对象设置为null将完成技巧和刷新会话但不删除数据库中的记录.

我的映射是......

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class name="StudioBusinessLayer.Data.Structure.Employee, StudioBusinessLayer" table="tblEmployee" lazy="false">
        <id name="ID" column="ID" type="int">
            <generator class="native" />
        </id>

        <property name="Name" column="Name" not-null="true" type="string" length="100" />
        <!-- etc -->

        <one-to-one constrained="false" name="EmployeeDetails" class="StudioBusinessLayer.Data.Structure.EmployeeDetails, StudioBusinessLayer" cascade="all-delete-orphan"  />

    </class>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)

...以及EmployeeDetails类......

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class name="StudioBusinessLayer.Data.Structure.EmployeeDetails, StudioBusinessLayer" table="tblDetails" lazy="false">
        <id name="ID" column="DetailsID" type="int">
            <generator class="foreign">
                <param name="property">Employee</param>
            </generator>
        </id>

        <property name="Address" column="Address" not-null="false" type="string" length="1000" />
        <property name="ContactEmail" column="ContactEmail" not-null="false" type="string" length="255" />
        <property name="ContactName" column="ContactName" not-null="false" type="string" length="255" />
        <property name="ContactTelephone" column="ContactTelephone" not-null="false" type="string" length="255" />
        <property name="ZipCode" column="ZipCode" not-null="true" type="string" length="100" />

        <many-to-one name="Employee" class="StudioBusinessLayer.Data.Structure.Employee, StudioBusinessLayer" column="DetailsID" insert="false" update="false"></many-to-one>

    </class>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)

插入和更新工作正常,但我一直在努力找到使其适用于删除的开关.

感谢任何帮助或建议......

Jak*_*art 2

不幸的是,NHibernate 中的一对一关系还不支持 all-delete-orphan。有关更多详细信息,请参阅此问题或此问题。似乎除了自己删除 EmployeeDetails 或使用事件侦听器来一对一模拟 all-delete-orphan 之外,没有其他方法了。