[英]What's the proper design for a one-to-one relationship using Fluent NHibernate?
I have a table of Recipes. 我有一张食谱表。 Each Recipe has one and only one row in table RecipeMetadata, which contains various data about the recipe that I don't want to store in the
Recipes
table for various reasons. 每个配方在表RecipeMetadata中只有一行,其中包含关于配方的各种数据,出于各种原因,这些数据我不想存储在
Recipes
表中。 Thus, Recipes
and RecipeMetadata
have a one-to-one mapping. 因此,
Recipes
和RecipeMetadata
具有一对一的映射。 My Recipes
table is as follows: 我的
Recipes
表如下:
public partial class RecipesMap : ClassMap<Recipes>
{
public RecipesMap()
{
Id(x => x.RecipeId);
// Map() various columns here
HasMany(x => x.Ingredients).KeyColumn("RecipeId");
HasOne(x => x.Metadata);
}
}
And here's my RecipeMetadata
table: 这是我的
RecipeMetadata
表:
public partial class RecipeMetadataMap : ClassMap<RecipeMetadata>
{
public RecipeMetadataMap()
{
Id(x => x.RecipeMetadataId);
// Map() various columns here
References<Recipes>(x => x.Recipe).Column("RecipeId").Not.Nullable();
}
}
However, when I load a Recipe
and access the Metadata
property, it attempts to find a row in RecipeMetadata
where Recipes.RecipeId = RecipeMetadata.RecipeMetadataId
. 但是,当我加载
Recipe
并访问Metadata
属性时,它会尝试在RecipeMetadata
中找到一行,其中Recipes.RecipeId = RecipeMetadata.RecipeMetadataId
。 In other words, it does the join using the primary keys on both tables. 换句话说,它使用两个表上的主键进行联接。
With my table schema, RecipeMetadataId
is a key unique only to that table, and has nothing to do with RecipeId
. 对于我的表架构,
RecipeMetadataId
是仅对该表唯一的键,与RecipeId
。 RecipeMetadata
has another column, also called RecipeId
which has a foreign key constraint on `Recipes. RecipeMetadata
具有另一列,也称为RecipeId
,该列对`Recipes有外键约束。 The JOIN should work as: JOIN的工作方式如下:
Recipes.RecipeId = RecipeMetadata.RecipeId
My Questions: 我的问题:
Am I wrong for wanting RecipeMetadata
to have its own unique ID, and to use a separate column to link this to Recipes
? 我是否希望
RecipeMetadata
具有自己的唯一ID,并使用单独的列将此链接到Recipes
这是错误的吗? Obviously, I have a FK constraint as well as a unique index on RecipeMetadata.RecipeId
so there's no perf impact. 显然,我具有FK约束以及
RecipeMetadata.RecipeId
上的唯一索引,因此不会影响性能。 Yes, there's some extra bytes on the disk for storing an arguably unnecessary ID on this table. 是的,磁盘上还有一些额外的字节,可以在此表上存储不必要的ID。
I've never seen a table whose primary key also has a foreign key constraint on another table. 我从未见过一个表,该表的主键在另一个表上也具有外键约束。 Is this legit practice?
这是合法的做法吗? It seems to be the way nHibernate prefers to behave by default.
这似乎是nHibernate在默认情况下更喜欢表现的方式。 Should I give in and let it have its way?
我应该让它让路吗?
Provided I don't want to change the database (Though I can be convinced to do so if given a legitimate reason), how can I create the desired one-to-one mapping with this model? 如果我不想更改数据库(虽然可以说服我,但如果有正当的理由也可以这么做),那么如何使用此模型创建所需的一对一映射?
NHibernate has a strict definition of one-to-one relationships. NHibernate对一对一关系有严格的定义。 Strict but fair.
严格但公平。 In NHibernate one-to-one relationship means that the a row in table A always has a matching row in table B.
在NHibernate中,一对一关系意味着表A中的行始终在表B中具有匹配的行。
See also: Ayende's post on the topic . 另请参阅: 有关该主题的Ayende帖子 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.