2009-10-17 87 views
4

複合ID我已經越過張貼這在谷歌FNH組,但這裏的獨家新聞:流利NHibernate和單列名

我有一個表具有複合ID舊的數據庫。那麼, PK是隱含的,因爲表實際上並沒有一個PK定義,但 自然PK是WORKORDERID和ImagePath的組合 列:

CREATE TABLE [WORKORDERIMG](
     [WORKORDERID] [varchar](50) NULL, 
     [IMAGEPATH] [varchar](250) NULL, 
     [WOTASKID] [numeric](10, 0) NULL, 
     [ATTACHEDBY] [nvarchar](100) NULL, 
     [COMMENTS] [nvarchar](256) NULL, 
     [DATETIMEATTACHED] [datetime] NULL 
) ON [PRIMARY] 

我都此類地圖定義:

/// <summary> 
    /// NHibernate mapping for workorder attachments 
    /// </summary> 
    public class AttachmentMap : ClassMap<Attachment> 
    { 
     public AttachmentMap() 
     { 
      SchemaIs(Resources.DatabaseSchema); 
      WithTable(ObjectNames.TableWorkorderAttachment); 
      UseCompositeId() 
       .WithKeyProperty(x => x.ParentId, "WORKORDERID") 
       .WithKeyProperty(x => x.FileLocation, "IMAGEPATH"); 
      Map(x => x.AttachedByUser, "ATTACHEDBY").WithLengthOf(100).Nullable(); 
      Map(x => x.AttachedOn, "DATETIMEATTACHED").Nullable(); 
      Map(x => x.Comments).Nullable().WithLengthOf(256); 
      References(x => x.ParentWorkorder).FetchType.Join(); 
     } 
    } 

對於這個類:

public class Attachment 
{ 
    public virtual string ParentId { get; set; } 
    public virtual string FileLocation { get; set; } 
    public virtual Workorder ParentWorkorder { get; set; } 
    public virtual string AttachedByUser { get; set; } 
    public virtual string Comments { get; set; } 
    public virtual DateTime? AttachedOn { get; set; } 

    public override bool Equals(object obj) 
    { 
     return this.IsEqual(obj); 
    } 

    public override int GetHashCode() 
    { 
     return HashCodeGenerator.GenerateHashCode(new object[] { FileLocation, ParentId }); 
    } 
} 

現在我知道我應該定義在t時的WORKORDERID列他也爲參考文件行提供 classmap。有了它排除我得到 預期的錯誤:

NHibernate: INSERT INTO azteca.WorkOrderImg (ATTACHEDBY, 
DATETIMEATTACHED, Comments, ParentWorkorder_id, WORKORDERID, 
IMAGEPATH) VALUES (@p0, @p1, @p2, @p3, @p4, @p5);@p0 = 'SYSTEM', @p1 = 3/15/2009 12:00:00 AM, @p2 = 'Some comment', @p3 = NULL, @p4 = NULL, @p5 = 'some ile\location' 

System.Data.SqlClient.SqlException: Invalid column name 'ParentWorkorder_id'. 

然而,當我把它給我弄功能NHibernate試圖將 WORKORDERID列添加到查詢兩次:

System.IndexOutOfRangeException: Invalid index 5 for this SqlParameterCollection with Count=5. 

我一直嘗試我想不出的每一種組合。我 測試的映射與此代碼:

 [Test] 
     public void Can_persist_an_attachment() 
     { 
      var sf = ObjectFactory.GetNamedInstance<ISessionFactory> (ObjectMother.Datasource); 

      using (ISession session = sf.OpenSession()) 
      { 
       new PersistenceSpecification<Attachment>(session) 
        .CheckProperty(x => x.AttachedByUser, "SYSTEM") 
        .CheckProperty(x => x.AttachedOn, new DateTime(2009, 3, 15)) 
        .CheckProperty(x => x.Comments, "Some comment") 
        .CheckProperty(x => x.FileLocation, "some\file\\location") 
        .VerifyTheMappings(); 
      } 
     } 

讚賞任何幫助。

回答

6

想通了。您不需要爲父級添加引用屬性。請注意調整後的映射中多對一缺失:

public AttachmentMap() 
{ 
    SchemaIs(Resources.CityworksDatabaseSchema); 
    WithTable(ObjectNames.TableWorkorderAttachment); 
    UseCompositeId() 
     .WithKeyReference(x => x.ParentWorkorder, "WORKORDERID") 
     .WithKeyProperty(x => x.FileLocation, "IMAGEPATH"); 
    Map(x => x.AttachedByUser, "ATTACHEDBY").WithLengthOf(100).Nullable(); 
    Map(x => x.AttachedOn, "DATETIMEATTACHED").Nullable(); 
    Map(x => x.Comments).Nullable().WithLengthOf(256); 
    //References(x => x.ParentWorkorder, "WORKORDERID").FetchType.Join(); 
} 

這會導致以下HBM映射文件。解決的辦法是讓composite-id有一個key-many-to-one行指向父類。現在我可以堅持我的Attachment類。

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access=""> 
    <class name="Woolpert.Cityworks.Core.Entities.Azteca.Attachment, Woolpert.Cityworks.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="WorkOrderImg" xmlns="urn:nhibernate-mapping-2.2" schema="azteca"> 
    <composite-id> 
     <key-many-to-one class="Woolpert.Cityworks.Core.Entities.Azteca.Workorder, Woolpert.Cityworks.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="ParentWorkorder" column="WORKORDERID" /> 
     <key-property type="String" name="FileLocation" column="IMAGEPATH" /> 
    </composite-id> 
    <property name="AttachedByUser" type="String"> 
     <column name="ATTACHEDBY" length="100" not-null="false" /> 
    </property> 
    <property name="AttachedOn" type="System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"> 
     <column name="DATETIMEATTACHED" not-null="false" /> 
    </property> 
    <property name="Comments" type="String"> 
     <column name="Comments" length="256" not-null="false" /> 
    </property> 
    </class> 
</hibernate-mapping>