请帮助我将这个简单流畅的 nhibernate 映射变为现实

发布于 2024-11-28 15:14:22 字数 1174 浏览 2 评论 0原文

好吧,这一切看起来都很简单,但我找不到问题的答案。

假设我有 2 个表 TableA 和 TableB,它们可以保存对第三个表的单个引用:

TableA       TableB        TableC
ID           ID            ID
TableC_ID    TableC_ID     RandomDataC
RandomDataA  RandomDataB

每个表都由以下类表示:

public Class ClassA{
  public virtual int Id{get;set;}
  public virtual ClassC ObjAC{get;set;}
  ...
}

public Class ClassB{
  public virtual int Id{get;set;}
  public virtual ClassC ObjBC{get;set;}
  ...
}

public Class ClassC{
  public virtual int Id{get;set;}
  ...
}

如您所见,ClassA 和 ClassB 保存一个名为 ClassC 的单个属性。

我已经尝试了以下映射(除其他外......),

 public Class ClassAMap : ClassMap<ClassA>{
    public ClassAMap(){
       Id(x => x.Id);
       HasOne(x => x.ObjAC).Cascade.All();
       ...
    }
 }

 public Class ClassBMap : ClassMap<ClassB>{
    public ClassBMap(){
       Id(x => x.Id);
       HasOne(x => x.ObjBC).Cascade.All();
       ...
    }
 }

 public Class ClassCMap : ClassMap<ClassC>{
    public ClassCMap(){
       Id(x => x.Id);
    }
 }

但映射不正确。我尝试使用 ClassCMap 来创建 ReferencesAny 但没有成功。

请帮忙...

Well it all seems so simple and yet I can't find the answer to my problem.

Let's say I have 2 tables TableA and TableB that can hold a single reference to a 3rd table:

TableA       TableB        TableC
ID           ID            ID
TableC_ID    TableC_ID     RandomDataC
RandomDataA  RandomDataB

Each are represented by the following classes:

public Class ClassA{
  public virtual int Id{get;set;}
  public virtual ClassC ObjAC{get;set;}
  ...
}

public Class ClassB{
  public virtual int Id{get;set;}
  public virtual ClassC ObjBC{get;set;}
  ...
}

public Class ClassC{
  public virtual int Id{get;set;}
  ...
}

As you can see ClassA and ClassB hold a single property called ClassC.

I've tried the following mapping (amongst others...)

 public Class ClassAMap : ClassMap<ClassA>{
    public ClassAMap(){
       Id(x => x.Id);
       HasOne(x => x.ObjAC).Cascade.All();
       ...
    }
 }

 public Class ClassBMap : ClassMap<ClassB>{
    public ClassBMap(){
       Id(x => x.Id);
       HasOne(x => x.ObjBC).Cascade.All();
       ...
    }
 }

 public Class ClassCMap : ClassMap<ClassC>{
    public ClassCMap(){
       Id(x => x.Id);
    }
 }

but the mapping is incorrect. I've tried ClassCMap to have a ReferencesAny but without success.

Please help...

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

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

发布评论

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

评论(3

烟酒忠诚 2024-12-05 15:14:22

试试这个:

public Class ClassAMap : ClassMap<ClassA>{
    public ClassAMap(){
       Id(x => x.Id);
       References(x => x.ObjAC, "TableC_ID").Cascade.All();
       ...
    }
 }

 public Class ClassBMap : ClassMap<ClassB>{
    public ClassBMap(){
       Id(x => x.Id);
       References(x => x.ObjBC, "TableC_ID").Cascade.All();
       ...
    }
 }

 public Class ClassCMap : ClassMap<ClassC>{
    public ClassCMap(){
       Id(x => x.Id);
    }
 }

您可能还想确保推断出正确的表名。问题在于 HasOne 根据您要映射的表的主键映射到实体。

Try this:

public Class ClassAMap : ClassMap<ClassA>{
    public ClassAMap(){
       Id(x => x.Id);
       References(x => x.ObjAC, "TableC_ID").Cascade.All();
       ...
    }
 }

 public Class ClassBMap : ClassMap<ClassB>{
    public ClassBMap(){
       Id(x => x.Id);
       References(x => x.ObjBC, "TableC_ID").Cascade.All();
       ...
    }
 }

 public Class ClassCMap : ClassMap<ClassC>{
    public ClassCMap(){
       Id(x => x.Id);
    }
 }

You may also want to make sure the correct table names are being inferred. The issue is that HasOne maps to an entity based on the primary key of the table you're mapping from.

梦毁影碎の 2024-12-05 15:14:22

当使用 HasOne 时,这意味着您的表与其他表共享相同的主键。就你而言,这不是真的。 A、B、C 类都有自己的 id。查看以下文章,更详细地解释了这一点:

http://jagregory.com/writings/i-think-you-mean-a-many-to-one-sir/

When using a HasOne this implies that your table shares the same primary key with the other table. In your case this is not true. Classes A, B, and C each have their own ids. Check out the following article that explains this in more detail:

http://jagregory.com/writings/i-think-you-mean-a-many-to-one-sir/

一曲爱恨情仇 2024-12-05 15:14:22

您可能需要使用 References 而不是 HasOne。 HasOne 有一个独特的用例。
HasOne 替换为 References。我也不认为 .Cascade.All() 是必要的。

您可以在此处查看 HasOne 的用法:http://wiki. Fluentnhibernate.org/Fluent_mapping

You 're probably needing to use References instead of HasOne. HasOne has a unique use case.
Replace HasOne with References. I don't believe .Cascade.All() is necessary either.

You can check out the usage for HasOne here: http://wiki.fluentnhibernate.org/Fluent_mapping.

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