我正在使用nhibernate映射舊數據庫,並在映射關係時遇到了一些問題。使用外鍵映射一個到多個集合到外鍵
這兩個類是這樣
public class Questionnaire
{
public int Id {get; set;}
public string FormCode {get; set;}
public IList<Question> Questions {get; set;}
}
public class Question
{
public int Id{get; set;}
public Questionnaire Questionnaire {get;set;}
public string QuestionText{get;set;}
}
到都是這樣
Questionnaire Table
Id int
FormCode varchar(100)
Question Table
Id int
FormCode varchar(100)
QuestionText varchar(max)
兩個表是所述formcode列之間的關係的表。
我當前的映射是這樣的
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="QDesign.Core.Models" assembly="QDesign.Core">
<class name="Questionnaire" table="_questionnaire_list">
<id column="Id" name="Id">
<generator class="identity"/>
</id>
<property name="FormCode" column="FormCode"/>
<bag name="Questions" >
<key foreign-key="FormCode" property-ref="FormCode" />
<one-to-many class="Question" />
</bag>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="QDesign.Core.Models" assembly="QDesign.Core">
<class name="Question" table="_questionnaire_items">
<id column="ID" name="Id" unsaved-value="-1">
<generator class="identity" />
</id>
<property name="QuestionText" column="QuestionText" />
</class>
</hibernate-mapping>
當我運行的映射,我得到一個標識符類型不匹配假設它試圖把formcode進入正題的ID。不幸的是,我無法改變表格的結構,我對如何繪製這張表格感到茫然,任何幫助都將不勝感激。
謝謝你的回覆。我會回到客戶端,看看我能否讓他們改變他們的模式。再次感謝您的回覆 – pythonandchips 2009-02-08 13:37:59
請記住,您不一定必須更改模式。這可能足以告訴Hibernate PK是FormCode(即使它不是)。 – cletus 2009-02-08 13:57:57