是否可以在流畅的 nhibernate 中映射复合键,其中键的一半是数据库中的身份字段?

发布于 2024-08-08 06:58:58 字数 158 浏览 4 评论 0原文

正如问题所述,是否可以在流利的 nhibernate(或我认为是非流利的)中映射组合键,其中组合中使用的两个字段之一是标识字段?

我有一张表,其中主键的一部分是身份字段,另一部分是租户 ID。这是一个遗留数据库,其中组合键全部用作外键,因此修改数据库将非常重要。

谢谢!

As the question states, is it possible to map a composite key in fluent nhibernate (or non fluent i suppose) where one of the two fields used in the composite is an identity field?

I have a table where one part of the primary key is an identity field and the other is a tenant id. This is a legacy database where the composite key is used as foreign keys all over, so modifying the database would be quite significant.

thanks!

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

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

发布评论

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

评论(1

掌心的温暖 2024-08-15 06:58:58

如果它确实是一个身份列,那么它保证每条记录都是唯一的,对吧?

如果是这样,您可以出于映射目的完全忽略租赁 ID。您的身份字段在主表中将是唯一的 - 并且您的外键引用将包含此唯一的 ID,因此租户 ID 实际上是不相关的。

NHibernate 并不真正关心您是否准确地映射了数据库模式。我之前曾使用过遗留数据库,我在非键列上定义了唯一约束,然后告诉 NHibernate 将其用作主键 - 它有效,并且可以巧妙地回避各种可怕的复合键关系映射。

想一想。违反直觉,但它可能会起作用。

If it's really an identity column, then it's guaranteed to be unique for every record, right?

If so, you can ignore the tenancy ID completely for mapping purposes. Your identity fields will be unique in your primary table - AND your foreign key references will contain this unique Id, so the tenancy ID is actually irrelevant.

NHibernate doesn't really care whether you're mapping your database schema exactly. I've worked with legacy databases before where I've defined a unique constraint on a non-key column and then told NHibernate to use that as the primary key - it works, and it can neatly sidestep all kinds of horrific composite-key relationship mappings.

Think about it. Counter-intuitive, but it will probably work.

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