2

我有以下课程:

public class Customer
{
    public virtual int CustomerID { get; protected set; }
    public virtual string AccountNumber { get; protected set; }
    public virtual string CustomerType { get; set; }
    public virtual int TerritoryID { get; set; }
    public virtual SalesTerritory Territory { get; protected set; }
}

它正在使用 Fluent NHibernate 进行映射,如下所示:

    public class CustomerMapper : ClassMap<Customer>
{
    private const string schema = "Sales";
    public CustomerMapper()
    {
        SchemaIs(schema);
        Id(x => x.CustomerID);
        Map(x => x.CustomerType).WithLengthOf(1).Not.Nullable();
        Map(x => x.TerritoryID).Not.Nullable();

        Map(x => x.AccountNumber).ReadOnly()
            .Update(false)
            .Insert(false)
            .Generated(Generated.Insert);

        References<SalesTerritory>(x => x.Territory).TheColumnNameIs(ReflectionHelper.GetProperty<Customer>(x => x.TerritoryID).Name).LazyLoad().Update(false).Insert(false);
    }
}

Update 和 Insert 方法只是在生成的映射文件中设置属性的更新插入属性的扩展方法。

这是生成的映射文件的外观:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true" assembly="Model" namespace="Model" schema="Sales">
    <class name="Customer" table="`Customer`" xmlns="urn:nhibernate-mapping-2.2">
        <id name="CustomerID" column="CustomerID" type="Int32">
            <generator class="identity" />
        </id>
        <property name="AccountNumber" column="AccountNumber" insert="false" update="false" generated="insert" length="100" type="String">
            <column name="AccountNumber" />
        </property>
        <property name="TerritoryID" column="TerritoryID" not-null="true" type="Int32">
            <column name="TerritoryID" />
        </property>
        <property name="CustomerType" column="CustomerType" not-null="true" length="1" type="String">
            <column name="CustomerType" />
        </property>
        <many-to-one lazy="proxy" update="false" insert="false" name="Territory" column="TerritoryID" />
    </class>
</hibernate-mapping>

我的问题是,当将新客户实例保存到数据库中时,客户实例的 Territory 属性为空。
保存到数据库后,如何使此属性包含正确的值?我正在查看类似于在刷新会话后检索正确值的属性的生成属性。
或者我的客户类有什么问题吗?
提前致谢。

编辑 1:我用于此模型的数据库是 AdventureWorks (SQL2005),表 Sales.Customer 和 Sales.SalesTerritory。

4

2 回答 2

2

更明智的映射如下:

public class CustomerMapper : ClassMap<Customer>
{
    private const string schema = "Sales";
    public CustomerMapper()
    {
        SchemaIs(schema);
        Id(x => x.CustomerID);
        Map(x => x.CustomerType).WithLengthOf(1).Not.Nullable();
        Map(x => x.AccountNumber).ReadOnly()
           .Generated(Generated.Insert);
        References(x => x.Territory).LazyLoad();
    }
}

几点:

  • 这算作一个映射的 SalesTerritory 类。
  • TerritoryID 不是必需的。
  • 如果您使用 Insert(false) 和 Update(false),这些属性将不会保存到数据库中。ReadOnly() 同时执行 Insert(false) 和 Update(false)。
  • CostumerType 完全可以是一个枚举。

这或多或少是一个想法(空气代码)。

于 2009-03-07T00:03:57.597 回答
0

我认为您的映射很奇怪...... regionId 属性与 Territory 属性的关系是什么?在多对一映射中,您映射 Territory 属性,您不必指定 Territory 的类型吗?(销售区域)。您将 2 个属性映射到同一列,为什么?

于 2009-03-06T19:11:04.447 回答