另一个 Fluent NH 加入问题

发布于 2024-10-07 12:24:17 字数 909 浏览 2 评论 0原文

我有一个网站。本网站位于特定时区并具有指定类型的互联网连接。这两个属性都作为单独查找表中记录的外键存储在数据库的站点表中。我永远不会这样做,但我明白为什么(强制使用一组通用的名称,提供稍小的数据库大小,在某些情况下允许标准化附加信息,并允许轻松检索值列表以用作DDL)。

我正在构建一个必须包含此信息检索的映射。我想避免必须构建一个域对象并为一个字段进行映射(在某些情况下,除了查找表的 ID 之外,这就是全部),所以我想对关系进行非规范化,然后只需要对象上的字符串值。简单的目标。

问题是我尝试使用的联接假设站点表的主键在查找表的某处被引用;实际上,我们假设网站位于关系的“一”方,这是不准确的。以下是相关的映射行:

Id(x=>x.Id).Column("SiteID");

...

Join("lu_TimeZone", j =>
{
   j.KeyColumn("TimeZoneID");
   j.Map(x => x.TimeZone).Column("TimeZone");
});

生成的 SQL 包括连接:

FROM Site this_0_ 
inner join lu_TimeZone this_0_1_ 
   on this_0_.SiteID=this_0_1_.TimeZoneID

*buzzer* 抱歉,这是不正确的,感谢您的参与。连接应该是:

FROM Site this_0_ 
inner join lu_TimeZone this_0_1_ 
   on this_0_.TimeZoneID=this_0_1_.TimeZoneID

但是,似乎没有办法告诉 Fluent 使用 KeyColumn 作为连接两侧的列名称。反转映射以将时区连接到站点是不可行的,因为从站点到其他查找表存在多个连接。

I have a Site. this Site resides in a particular TimeZone and has a specified type of Internet connection. Both of these properties are stored in the Site table of the DB as foreign keys to records in a seperate lookup table. I would never have done it that way, but I understand why (enforces a common set of names, gives a SLIGHTLY smaller DB size, allows normalized additional information in some cases, and allows for easy retrieval of the list of values for use as a DDL).

I'm building a mapping that must include retrieval of this information. I would like to avoid having to build a domain object and mapping for one field (which, in some cases other than the ID of the lookup table, that's all there is), so instead I'd like to denormalize the relationship and just have the string value on the object. Simple goal.

The problem is that the Join I'm attempting to use assumes that the Site table's primary key is referenced somewhere on the lookup table; in effect, we're assuming the Site is on the "one" side of the relationship, which is inaccurate. Here are the relevant mapping lines:

Id(x=>x.Id).Column("SiteID");

...

Join("lu_TimeZone", j =>
{
   j.KeyColumn("TimeZoneID");
   j.Map(x => x.TimeZone).Column("TimeZone");
});

The SQL generated includes the join:

FROM Site this_0_ 
inner join lu_TimeZone this_0_1_ 
   on this_0_.SiteID=this_0_1_.TimeZoneID

*buzzer* Sorry, that's incorrect, thanks for playing. The join should be:

FROM Site this_0_ 
inner join lu_TimeZone this_0_1_ 
   on this_0_.TimeZoneID=this_0_1_.TimeZoneID

However there seems to be no way to tell Fluent to use the KeyColumn as the column name on both sides of the join. Reversing the mapping to join TimeZone to Site is infeasible as there are multiple joins from Site to other lookup tables.

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

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

发布评论

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

评论(1

画尸师 2024-10-14 12:24:17

作为墓志铭,我最终做了以下两件事之一:

  • 对于只有一个可能感兴趣的字段的表,我创建了一个以表 ID 为键的枚举,并使用自定义类型。

  • 对于可能有多个值的表,我创建并映射了一个域对象。

无论如何,如果不将子表引用为对象,就不可能使上述连接行为起作用。

As an epitaph, I ended up doing one of two things:

  • For tables where there was only one field that could ever be of interest, I created an enum keyed to the ID of the table, and mapped them as Enums using a custom type.

  • For tables where there could be more than one value, I created and mapped a domain object.

It may be impossible to get the joining behavior as described above to work without referencing the subtable as an object anyway.

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