开发者

Fluent NHibernate misplaced duplicate field

开发者 https://www.devze.com 2023-01-06 11:09 出处:网络
I think I\'m misunderstanding something about how this works. This is my fluent mapping: public class FunctionInfoMap : ClassMap<FunctionInfo>

I think I'm misunderstanding something about how this works. This is my fluent mapping:

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");
  }
 }

Now when I create this schema into a fresh database, that FunctionId field from SampleMap winds up in the Calls table.

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)
)

I don't understand why it's there, since it should only exist in the Samples table.


You should not map both the foreign key and the many-ton-one relationship. Instead of

   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 the many-to-ones which use the foreign keys

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

I don't know if this will solve your problem. Also, I strongly advise against using composite keys. Replace them with surrogate key (identity) and unique indexes.


I finally figured out the problem, more or less. The pair of collections on FunctionInfoMap were confusing NHibernate, since they don't actually lead anywhere. Adding KeyColumn entries to link them up to the Parent and Child associations corrected the stray field.

0

精彩评论

暂无评论...
验证码 换一张
取 消