2011-10-31 76 views
0

我已經嘗試了許多不同的映射配置,但繼續生成異常或創建/更新錯誤的記錄。使用NHibernate引用子類映射與Fluent

例外

  • 不能在表中插入的標識列的顯式值
  • 不能使用標識列密鑰生成與工會子類映射
  • 類XXX產生
  • 空ID

當我有一個保存的地圖,我有以下問題

我已成功插入並更新數據庫中的記錄,但這些記錄沒有適當的ID。他們都有一個ID爲0,因此只會一遍又一遍地更新相同的記錄。

問題我想解決

我試圖SubclassMap接口IRequest。此接口用作單獨的類AbstractWorkflowRequestInformation上的屬性。保存父類時,我想將引用的IRequest保存在適當的子類表中。這是我目前的映射,它會生成例外Cannot insert explicit value for identity column in table。我相信我有一些東西在我繪製這兩個類之間關係的方式上做了調整。我究竟做錯了什麼?我的地圖如下。這裏

IRequestMap

public class IRequestMap : ClassMap<IRequest> 
{ 
    public IRequestMap() 
    { 
     Id(x => x.WorkflowRequestInformation) 
      .GeneratedBy.Foreign("AbstractWorkflowRequestInformation"); 
     UseUnionSubclassForInheritanceMapping(); 
    } 
} 

public class PlanRequestMap : SubclassMap<PlanRequest> 
{ 
    public PlanRequestMap() 
    { 
     Table("plan_request"); 
     // specific to PlanRequest property mappings and references 
    } 
} 

public class BnjRequestMap : SubclassMap<BnjRequest> 
{ 
    public BnjRequestMap() 
    { 
     Table("scratchdb.guest.bnj_request"); 
     // specific to BnjRequest property mappings and references 
    } 
} 

AbstractWorkflowRequestInformationMap

public class AbstractWorkflowRequestInformationMap : 
    ClassMap<AbstractWorkflowRequestInformation> 
{ 
    public AbstractWorkflowRequestInformationMap() 
    { 
     Table("workflow_request_information"); 
     Id(x => x.Id) 
      .Column("workflow_request_information_id") 
      .GeneratedBy.Identity(); 

     // more property mappings and references 
     References(x => x.SuperDuperRequest, "workflow_request_information_id") 
      .Class<IRequest>().Unique().Cascade.All(); 
     // more property mappings and references 
    } 
} 
+0

完整的例外將有助於看到:哪個表的身份,同時插入哪個實體等等 – Firo

回答

0

的問題是,我的IRequest地圖是缺少約束參考回到AbstractWorkflowRequestInformation。所以通過添加一個WorkflowRequestInformation屬性和約束條件,現在所有的東西都可以按照我的意圖工作。

public class IRequestMap : ClassMap<IRequest> 
{ 
    public IRequestMap() 
    { 
     // adding this fixed the problem 
     HasOne(x => x.WorkflowRequestInformation).Constrained(); 
     // adding this fixed the problem 

     Id(x => x.WorkflowRequestInformation) 
      .GeneratedBy.Foreign("AbstractWorkflowRequestInformation"); 
     UseUnionSubclassForInheritanceMapping(); 
    } 
} 
相關問題