为什么NHibernate使用类名作为列生成模式?

时间:2010-11-29 20:17:43

标签: nhibernate nhibernate-mapping

我正在尝试使用NHibernate从类生成我的数据库,但是正在创建的模式包含该类的列。在大多数情况下,这不重要,但特别是一个类导致它失败...我的Order类(因为order是SQL中的保留关键字 - 是的,我可能应该使用一个不是关键字的单词但是这是另一个问题)

以下映射文件:

<?xml version="1.0" encoding="utf-8" ?>

<class name="Order" table="Orders">
    <id name="_persistenceID" column="ID" type="Guid" access="field" unsaved-value="00000000-0000-0000-0000-000000000000">
        <generator class="guid.comb" />
    </id>
    <version name="_persistenceVersion" column="Version" access="field" type="int" unsaved-value="0" />

    <property name="DateDeleted" column="DateDeleted" type="DateTime" />
    <property name="ChangeReason" column="ChangeReason" type="String" not-null="true" />
    <property name="CompletedDate" column="CompletedDate" type="DateTime" />
    <property name="IsDeleted" column="IsDeleted" type="Boolean" />
    <property name="Notes" column="Notes" type="String" />
    <property name="OrderDate" column="OrderDate" type="DateTime" not-null="true" />
    <property name="OrderTotal" column="OrderTotal" type="Decimal" />
    <property name="RequiredDate" column="RequiredDate" type="DateTime" />

    <many-to-one name="Customer" column="Customer" class="Customer" not-null="true" />

    <bag name="OrderDetails" table="OrderDetails" generic="true" cascade="all">
        <key column="OrderDetail" foreign-key="FK_OrderOrderDetails" />
        <one-to-many class="OrderDetail" />
    </bag>

</class>

生成以下SQL:

    create table Orders (
  ID UNIQUEIDENTIFIER not null,
   Version INT not null,
   DateDeleted DATETIME null,
   ChangeReason NVARCHAR(255) not null,
   CompletedDate DATETIME null,
   IsDeleted BIT null,
   Notes NVARCHAR(255) null,
   OrderDate DATETIME not null,
   OrderTotal DECIMAL(19,5) null,
   RequiredDate DATETIME null,
   Customer UNIQUEIDENTIFIER not null,
   Order UNIQUEIDENTIFIER null,
   primary key (ID)
)

这是导致此无效并突出显示问题的倒数第二列(订单)。它发生在我所有其他课程上 - 但我不知道为什么。我该怎么办呢?

编辑:30/11/2010 - 映射文件的另一端:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="TheWorkshop.DomainModel" namespace="TheWorkshop.DomainModel" default-access="field.camelcase-underscore" default-lazy="true">

<class name="OrderDetail" table="OrderDetails">
    <id name="_persistenceID" column="ID" type="Guid" access="field" unsaved-value="00000000-0000-0000-0000-000000000000">
        <generator class="guid.comb" />
    </id>
    <version name="_persistenceVersion" column="Version" access="field" type="int" unsaved-value="0" />

    <property name="AdjustedPrice" column="AdjustedPrice" type="Decimal" not-null="true" />
    <property name="LineCost" column="LineCost" type="Decimal" not-null="true" />
    <property name="Notes" column="Notes" type="String" />
    <property name="Photo" column="Photo" type="String" />
    <property name="Price" column="Price" type="Decimal" not-null="true" />
    <property name="Quantity" column="Quantity" type="int" />

    <many-to-one name="`Order`" column="`Order`" class="Order" not-null="true" />
    <many-to-one name="Product" column="Product" class="Product" not-null="true" />

</class>

并生成以下内容:

create table OrderDetails (
ID UNIQUEIDENTIFIER not null,
Version INT not null,
AdjustedPrice DECIMAL(19,5) not null,
LineCost DECIMAL(19,5) not null,
Notes NVARCHAR(255) null,
Photo NVARCHAR(255) null,
Price DECIMAL(19,5) not null,
Quantity INT null,
[Order] UNIQUEIDENTIFIER not null,
Product UNIQUEIDENTIFIER not null,
primary key (ID)
)

1 个答案:

答案 0 :(得分:0)

我认为问题是将关键列设置为OrderDetail。 column属性应该包含OrderDetail中Order表中的外键列的名称,我怀疑你是否已将该列命名为OrderDetail。我的猜测是它应该是OrderID。当提供类名而不是列名时,可能是模式生成中的错误。

<key column="OrderDetail" foreign-key="FK_OrderOrderDetails" />