2012-03-15 37 views
0

我有以下情況:組合模型插入問題

我有2個班(兒童)和複合類(父):

public Parent 
{ 
    public ChildOne C1 {get;set;} 
    public ChildTwo C2 {get;set;} 
} 

public ChildOne 
{ 
    public int Id{get;set;} 
...some primitive fields 
} 

public ChildTwo 
{ 
    public int Id{get;set;} 
...other primitive fields 
} 

這些類的映射是以下:

public class Parent: ClassMap<Parent> 
    { 
     public ParentMap() 
     { 
      Table("Parents"); 
      Id(x => x.Id).Column("ParentId"); 
      References(x => x.C1).Column("ChildOneId"); 
      References(x => x.C2).Column("ChildTwoId"); 
     } 
    } 

public class ChildOne: ClassMap<ChildOne> 
    { 
     public ChildOneMap() 
     { 
      Table("ChildOnes"); 
      Id(x => x.Id).Column("ChildOneId"); 
      ...other Mappings for primitive types 
     } 
    } 

(對於ChildTwo也是如此)。

的DB如下:

Parents: ParentId, ChildOneId, ChildTwoId 
ChildOne: ChildOneId, ...other columns 
ChildOne: ChildTwoId, ...other columns 

和關係ChildOneId(父母)< ==> ChildOneId(ChildOnes)。

當我想要在相應的孩子的Db中插入一個新的Parent時,我得到一個ChildOneId/ChildTwoId不能爲null的錯誤。

爲了使插入工作,我需要在映射類中指定什麼? (檢索操作正常工作)

感謝, Tamash

+0

您可以發佈您用於創建父對象的代碼以及它如何填充ChildOne&ChildTwo屬性? – 2012-03-15 14:19:31

回答

1

我猜你不調用保存上孩子的。

session.Save(new Parent { C1 = new ChildOne(), C2 = new ChildTwo() }); 

使用級聯來執行從父級保存子級。

References(x => x.C1).Column("ChildOneId").Cascade.All(); 
References(x => x.C2).Column("ChildTwoId").Cascade.All(); 
+0

工作就像一個魅力!非常感謝! – 2012-03-23 23:45:31