【问题标题】:How to include a mapping as a base to a new mapping?如何将映射作为新映射的基础?
【发布时间】:2012-06-29 14:33:57
【问题描述】:

是否可以使用来自引用程序集的映射作为新映射的基础?

这背后的原因是许多应用程序使用通用对象,因此它们引用 CentralDataLayer.dll。问题是某些应用程序具有与 CentralDataLayer 中的表相关的附加表。所以我希望能够从这些应用程序中扩展这些映射。

引用的基本映射看起来像往常一样:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="CentralDataLayer" namespace="NHibernate.Map">
    <class name="NHibernate.Map.Employee, CentralDataLayer" table="`wt_employee`" schema="`wt_global`">

        <id name="Id" column="`emp_id`" type="Int32" unsaved-value="0">
            <generator class="native" />
        </id>

        <property name="Number" column="`emp_number`" type="Int32" not-null="true" />
        <property name="Email" column="`email`" type="String" length="100" />
        <property name="IsFullTime" column="`is_full_time`" type="Boolean" not-null="true" />
        <property name="HireDate" column="`hire_date`" type="Date" />

        <property name="SsnSerialNumber" column="`last4ssn`" type="String" lazy="true" />

        <property name="OfficePhone" column="`work_phone`" type="String" length="10" />
        <property name="OfficeExtension" column="`work_phone_extn`" type="String" length="5" />

        <component name="UpdateRecord" class="NHibernate.Map.Component.UpdateRecord, CentralDataLayer">
            <property name="Date" column="`updated_dt`" type="Date" />
            <property name="By" column="`updated_by`" type="String" length="60" />
        </component>

        <many-to-one name="User" column="`user_id`" class="NHibernate.Map.User, CentralDataLayer" unique="true" />
        <many-to-one name="Job" column="`job_id`" class="NHibernate.Map.Job, CentralDataLayer" />

    </class>
</hibernate-mapping>

下一个映射文件是我不确定的:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DataLayer" namespace="NHibernate.Map.WIP" auto-import="false">
    <class name="NHibernate.Map.WIP.Employee, DataLayer" table="`wt_employee`" schema="`wt_wip`">

        <!-- Logically I would do something like this. -->
        <include class="NHibernate.Map.Employee" table="`wt_employee`" schema="`wt_global`" />

        <property name="Active" column="`emp_active`" type="Boolean" not-null="true" />
        <property name="DashboardColumnAmount" column="`emp_dashboard_col_amount`" type="Byte" not-null="true" />

    </class>
</hibernate-mapping>

NHibernate.Map.WIP.Employee 映射的结果类将是:

namespace NHibernate.Map.WIP
{
    public class Employee : Map.Employee
    {
        #region Fields

        private bool _active;
        private byte _dashboardColumnAmount;

        #endregion Fields

        #region Properties

        public virtual bool Active
        {
            get { return _active; }
            set { _active = value; }
        }
        public virtual byte DashboardColumnAmount
        {
            get { return _dashboardColumnAmount; }
            set { _dashboardColumnAmount = value; }
        }

        #endregion Properties
    }
}

现在开发人员应该能够在项目中使用 NHibernate.Map.WIP.Employee 并连接 NHibernate.Map.Employee 数据。

【问题讨论】:

  • 为什么不将其映射为 Employee 的子类?
  • 如何在不触及 NHibernate.Map.Employee 映射的情况下做到这一点?

标签: c#-4.0 nhibernate-mapping partial-classes


【解决方案1】:

AFAIK 你不能。您可以加载映射 xml 并在将其提供给会话工厂之前对其进行更改,或者您可以将其移植到 FluentNHibernate,然后您可以根据需要添加子类。

【讨论】:

    猜你喜欢
    • 2018-08-25
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 2012-02-20
    相关资源
    最近更新 更多