简体   繁体   English

使用Fluent NHibernate进行一对一关系的正确设计是什么?

[英]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. 因此, RecipesRecipeMetadata具有一对一的映射。 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: 我的问题:

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

  2. 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? 我应该让它让路吗?

  3. 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中具有匹配的行。

  1. Right or wrong, that won't work with NHibernate's one-to-one mapping. 对与错,这将不适用于NHibernate的一对一映射。 Note that the model you propose is identical to how a one-to-many relationship would be modeled. 请注意,您建议的模型与一对多关系的建模方式相同。
  2. It's legit and enforces the one-to-one relationship. 合法并强制一对一关系。
  3. Since you want the recipe to always have an associated metadata row, I would model it using NHibernate's one-to-one mapping. 由于您希望配方始终具有关联的元数据行,因此我将使用NHibernate的一对一映射对其进行建模。 Alternatively, you can map it as one-to-many but only expose one instance as a property. 另外,您可以将其映射为一对多,但仅将一个实例公开为属性。

See also: Ayende's post on the topic . 另请参阅: 有关该主题的Ayende帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM