Fluent NHibernate 错位重复字段

发布于 2024-09-09 07:18:48 字数 1932 浏览 2 评论 0原文

我想我误解了它的工作原理。这是我的流畅映射:

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 字段将出现在 Calls 表中。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓬勃野心 2024-09-16 07:18:48

您不应同时映射外键和多吨一关系。 而不是

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

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

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.

银河中√捞星星 2024-09-16 07:18:48

我终于或多或少地弄清楚了问题所在。 FunctionInfoMap 上的这对集合让 NHibernate 感到困惑,因为它们实际上并不指向任何地方。添加 KeyColumn 条目将它们链接到父级和子级关联更正了杂散字段。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文