使用 NHibernate 和外键映射 SQL 视图

发布于 2025-01-08 11:13:55 字数 1002 浏览 3 评论 0原文

在我的解决方案中,我有以下项目。

MySystem.Core
MySystem.Core.Data

MySystem.MyAudit1.Core
MySystem.MyAudit1.Core.Data

MySystem.MyAudit2.Core
MySystem.MyAudit2.Core.Data

审计项目的数量可能会增加到 20 个左右。

审计项目都需要“组织”参考数据表。我的计划是在核心项目中拥有一个“主”组织表,而不是在所有审计项目中重复 20 次相同的参考数据表并尝试使它们保持同步。

这将包含所有组织的代码和描述。然后,在每个特定的审计项目中都有一个组织代码表,其中仅包含与该审计相关的组织代码,但不包含描述,并且在审计数据库中有一个视图,可以从核心组织表中查找描述,如下所示:

CREATE VIEW Organisation AS
SELECT d.OrganisationCodeId as 'OrganisationId', d.Code, a.[Description]
FROM MyAudit1.dbo.OrganisationCode d
INNER JOIN [Core].dbo.OrganisationCode a ON d.Code = a.Code

这意味着如果组织描述发生变化,可以在一处进行更新并反映在所有审核中。

但是,由于您不能在 SQL 中的视图上拥有外键,因此我在 OrganizationCode 表和审计数据库中需要它们的其他表之间创建了一个外键。

正如我所发现的,这当然会导致 NHibernate 尝试返回 OrganizationCode table 而不是 Organization view ,并导致我的 SessionFactory 创建失败,因为 Nhibernate 正在寻找 OrganizationId组织代码表。

有没有一种简单的方法可以解决这个问题,它允许我在核心项目中进行单个描述查找,以获取可跨子项目使用的参考数据

In my Solution I have the following projects

MySystem.Core
MySystem.Core.Data

MySystem.MyAudit1.Core
MySystem.MyAudit1.Core.Data

MySystem.MyAudit2.Core
MySystem.MyAudit2.Core.Data

The number of audit projects could potentially grow to around 20.

The Audit projects all require an "Organisation" reference data table. Rather than duplicate the same reference data table across all audit projects 20 times and try to keep them in sync, my plan is to have a "master" organisation table in the Core project.

This will contain Code and Description for all organisations. Then, in each particular audit project have an OrganisationCode table that contains only the Org Codes relevant to that audit but not the description, and have a view in the audit database that looks up the description from the core Org table as follows:

CREATE VIEW Organisation AS
SELECT d.OrganisationCodeId as 'OrganisationId', d.Code, a.[Description]
FROM MyAudit1.dbo.OrganisationCode d
INNER JOIN [Core].dbo.OrganisationCode a ON d.Code = a.Code

This will mean that if Org descriptions change they can be updated in one place and reflected across all the audits.

However, as you cannot have foreign keys on Views within SQL I had created a foreign key between the OrganisationCode table and whichever other tables in the audit database needs them.

As I have discovered, this of course causes NHibernate to try and return OrganisationCode table rather than the Organisation view and causes my SessionFactory creation to fall over as Nhibernate is looking for OrganisationId in the OrganisationCode table.

Is there an easy way round this which will allow me to have a single description lookup in the core project for reference data that can be used across child projects

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

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

发布评论

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

评论(1

段念尘 2025-01-15 11:13:55

是的,审计表分布在单独的数据库中,但我已经找到了问题的根源。

我的 SessionFactory 创建失败的原因是配置正在通过审核数据程序集进行映射,而我的组织映射文件位于核心数据程序集中。

现在,我已经通过在审计数据程序集中添加组织映射文件解决了这个问题,我的信任对象正在从组织视图返回描述值。

感谢您的关注。

Yes the audit tables are spread over separate dbs, but I have got to the route of my problem.

The reason that my SessionFactory creation was falling over is that the Configuration was being passed the audit data assembly for mapping, and my organisation mapping file was in the core data assembly.

Now that I have solved that by adding an organisation mapping file in my audit data assembly, my trust object is returning the description values from the organisation view.

Thanks for looking.

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