2010-07-09 37 views
0

我想我误解了一些关于它是如何工作的。这是我的流利的映射:流利的NHibernate错位复制域

public class FunctionInfoMap : ClassMap<FunctionInfo> 
{ 
    public FunctionInfoMap() 
    { 
    Id(x => x.Id); 
    Map(x => x.Name); 
    Map(x => x.Signature); 
    Map(x => x.IsNative); 
    Map(x => x.ClassId); 
    References(x => x.Class, "ClassId"); 
    HasMany(x => x.CallsAsParent).Inverse(); 
    HasMany(x => x.CallsAsChild).Inverse(); 
    Table("Functions"); 
    } 
} 

public class CallMap : ClassMap<Call> 
{ 
    public CallMap() 
    { 
    CompositeId() 
    .KeyProperty(x => x.ThreadId) 
    .KeyProperty(x => x.ParentId) 
    .KeyProperty(x => x.ChildId) 
    .Mapped(); 
    Map(x => x.ThreadId).Index("Calls_ThreadIndex"); 
    Map(x => x.ParentId).Index("Calls_ParentIndex"); 
    Map(x => x.ChildId).Index("Calls_ChildIndex"); 
    Map(x => x.HitCount); 
    References(x => x.Thread, "ThreadId"); 
    References(x => x.Parent, "ParentId"); 
    References(x => x.Child, "ChildId"); 
    Table("Calls"); 
    } 
} 

public class SampleMap : ClassMap<Sample> 
{ 
    public SampleMap() 
    { 
    CompositeId() 
    .KeyProperty(x => x.ThreadId) 
    .KeyProperty(x => x.FunctionId) 
    .Mapped(); 
    Map(x => x.ThreadId); 
    Map(x => x.FunctionId); 
    Map(x => x.HitCount); 
    References(x => x.Thread, "ThreadId"); 
    References(x => x.Function, "FunctionId"); 
    Table("Samples"); 
    } 
} 

现在,当我创建此架构到一个新的数据库,从SampleMap是FunctionId场在呼叫表卷起。

create table Calls (
     ThreadId INTEGER not null, 
     ParentId INTEGER not null, 
     ChildId INTEGER not null, 
     HitCount INTEGER, 
     FunctionId INTEGER, 
     primary key (ThreadId, ParentId, ChildId) 
    ) 

create table Samples (
    ThreadId INTEGER not null, 
    FunctionId INTEGER not null, 
    HitCount INTEGER, 
    FunctionId INTEGER, 
    primary key (ThreadId, FunctionId) 
) 

我不明白为什么它存在,因为它应该只存在于Samples表中。

回答

0

我终于找出了问题,或多或少。 FunctionInfoMap上的这对集合让NHibernate感到困惑,因为它们实际上并不在任何地方。添加KeyColumn条目以将它们链接到Parent和Child关联可更正杂散字段。

0

您不应该同时映射外键和多重关系。取而代之的

Map(x => x.ThreadId).Index("Calls_ThreadIndex"); 
    Map(x => x.ParentId).Index("Calls_ParentIndex"); 
    Map(x => x.ChildId).Index("Calls_ChildIndex"); 
    Map(x => x.HitCount); 
    References(x => x.Thread, "ThreadId"); 
    References(x => x.Parent, "ParentId"); 
    References(x => x.Child, "ChildId"); 

映射多对一酮,它们使用外键

Map(x => x.HitCount); 
    References(x => x.Thread, "ThreadId"); 
    References(x => x.Parent, "ParentId"); 
    References(x => x.Child, "ChildId"); 

我不知道这是否会解决您的问题。另外,我强烈建议不要使用复合键。用替代键(标识)和唯一索引替换它们。

+0

不幸的是,杂散场仍然存在。 – Promit 2010-07-09 17:33:09