Fluent-NHibernate:如何将一对多关系映射到3个表中(多对多,有点)
我有像这样的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我理解正确,那么您正在创建一个引用表,因为您希望引用可以为空。如果是这种情况,则不需要参考表。只需将表 b 中的 FK 设置为可为空即可。然后你可以将它映射为一个简单的参考。然后你会有这样的表:
Aid (可为空)
你可以像这样映射它:
更新
在 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:
Aid (nullable)
And you can map it like this:
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.
这是典型的多对多关系。在 FluentNHibernate 中,您可以使用
http://wiki. Fluentnhibernate.org/Fluent_mapping#HasManyToMany_ .2F_多对多
This is a typical ManyToMany relationship. In FluentNHibernate you would use
http://wiki.fluentnhibernate.org/Fluent_mapping#HasManyToMany_.2F_many-to-many
你想要的是 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
您不应该将一对多关系映射为多对多关系,然后强制其成为一对多关系。它的设计很糟糕,并且不利于数据完整性。如果我是您,我会按如下方式创建表
这样,您就可以按照您想要的方式在 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
That way, you can map them the way you want to in Fluent NHibernate, or any other ORM.