Fluent-NHibernate:如何将一对多关系映射到3个表中(多对多,有点)

发布于 2024-08-18 15:01:04 字数 360 浏览 5 评论 0原文

我有像这样的 2 个实体:

class A
{
   int id { get; set; }
   string Name { get; set; }
}

class B
{
   int id { get; set; }
   A RefToA { get; set; }
   string Name { get; set; }
}

如何映射这 2 个类,以便我有 3 个这样的表:

  • 具有 2 列的表 A:id 和 name

  • 表 B 具有 2 列:id 和名称

  • 表 AB 具有 2 列:AId 和 BId

I have to 2 entities like this:

class A
{
   int id { get; set; }
   string Name { get; set; }
}

class B
{
   int id { get; set; }
   A RefToA { get; set; }
   string Name { get; set; }
}

How can I map this 2 classes so that i would have 3 tables like this:

  • table A with 2 columns: id and name

  • table B with 2 columns: id and name

  • table AB with 2 columns: AId and BId

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

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

发布评论

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

评论(4

冰之心 2024-08-25 15:01:04

如果我理解正确,那么您正在创建一个引用表,因为您希望引用可以为空。如果是这种情况,则不需要参考表。只需将表 b 中的 FK 设置为可为空即可。然后你可以将它映射为一个简单的参考。然后你会有这样的表:

  • 表 A 有 2 列:id 和 name
  • 表 B 有 2 列:id、name 和
    Aid (可为空)

你可以像这样映射它:

public class AMap : ClassMap<A>
{
  public AMap()
  {
      Id(x => x.Id); 
      Map(x => x.Name);
  }
}

public class BMap : ClassMap<B>
{
  public BMap()
  {
     Id(x => x.Id); 
     Map(x => x.Name);
     References(x => x.RefToA);
  }
}

更新

在 nhibernate 中没有办法按照你想要的方式映射它(并且没有其他形式)。原因很简单:它违反了很多规则,而且没有理由这样做。正确的方法是在表 b 中有一个可为空的 fk 引用。这就是在 sql 数据库中表示引用的方式。当您的意思是一对多时,使用多对多简直是糟糕的设计,并且它肯定会给您带来以后的麻烦。

If I understand this correct you are creating a ref table because you want the reference to be nullable. If that is the case, you do not need a ref table. Simply set the FK in table b as nullable. Then you can map it a simple reference. Then you would have tables like this:

  • table A with 2 columns: id and name
  • table B with 2 columns: id, name and
    Aid (nullable)

And you can map it like this:

public class AMap : ClassMap<A>
{
  public AMap()
  {
      Id(x => x.Id); 
      Map(x => x.Name);
  }
}

public class BMap : ClassMap<B>
{
  public BMap()
  {
     Id(x => x.Id); 
     Map(x => x.Name);
     References(x => x.RefToA);
  }
}

Update

There is no way of mapping this how you want in nhibernate (and no other orm for that matter). The reason for this is quite simple: it violates quite a few rules and there is never a reason to do it this way. The correct way to do this is to have a nullable fk reference in table b. That is how you represent a reference in a sql database. It is simply bad design to use many-to-many when you mean one-to-many and it will most certainly give you trouble later on.

一指流沙 2024-08-25 15:01:04

这是典型的多对多关系。在 FluentNHibernate 中,您可以使用

HasManyToMany<T>();

http://wiki. Fluentnhibernate.org/Fluent_mapping#HasManyToMany_ .2F_多对多

This is a typical ManyToMany relationship. In FluentNHibernate you would use

HasManyToMany<T>();

http://wiki.fluentnhibernate.org/Fluent_mapping#HasManyToMany_.2F_many-to-many

软糯酥胸 2024-08-25 15:01:04

你想要的是 References(x => x.RefToA);

以下内容来自 Fluent nhibernate 文档。

引用/多对一

引用用于在两个实体之间创建多对一关系;您正在引用另一个实体,因此您使用 References 方法。引用是对实体的单个实例

http://wiki. Fluentnhibernate.org/Fluent_mapping

what you want is References(x => x.RefToA);

The following below is from the fluent nhibernate documenation.

References / many-to-one

References is for creating many-to-one relationships between two entities; you're referencing another entity, so you use the References method. A reference is to a single instance of an entity

http://wiki.fluentnhibernate.org/Fluent_mapping

︶葆Ⅱㄣ 2024-08-25 15:01:04

您不应该将一对多关系映射为多对多关系,然后强制其成为一对多关系。它的设计很糟糕,并且不利于数据完整性。如果我是您,我会按如下方式创建表

CREATE TABLE A(
    Id INT PRIMARY KEY IDENTITY(1,1),
    --Your other columns
)

CREATE TABLE B(
    Id INT PRIMARY KEY IDENTITY(1,1),
    AId INT REFERENCES A NULL,
    --Your other columns
)

这样,您就可以按照您想要的方式在 Fluent NHibernate 或任何其他 ORM 中映射它们。

You should not be mapping a one-to-many relation as a many-to-many relation, then forcing it to be a one-to-many. Its bad design, and does not promote data integrity. If I were you, I would create your tables as follows

CREATE TABLE A(
    Id INT PRIMARY KEY IDENTITY(1,1),
    --Your other columns
)

CREATE TABLE B(
    Id INT PRIMARY KEY IDENTITY(1,1),
    AId INT REFERENCES A NULL,
    --Your other columns
)

That way, you can map them the way you want to in Fluent NHibernate, or any other ORM.

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