NHibernate 遗留数据库映射不可能吗?

发布于 2024-08-25 08:28:16 字数 782 浏览 10 评论 0原文

我希望有人可以帮助我映射旧数据库。这 我在这里描述的问题已经困扰了其他人,但我无法解决 在网络上找到真正好的解决方案。

免责声明:这是一个遗留数据库。我无法控制复合 键。它们很糟糕,无论你告诉我多少,都无法改变 糟糕。我也无法添加代理键。请不要建议其中任何一个,因为它们不是选项。

我有 2 个表,都有复合键。一把钥匙中的一把 表用作从另一个获取集合的键的一部分 桌子。简而言之,表之间的键并不完全匹配。 ClassB 用于我想避免为其添加属性的任何地方 如果可能的话,为了这个映射。

public class ClassA
{
    //[PK]
    public string SsoUid;

    //[PK]
    public string PolicyNumber;

    public IList<ClassB> Others;

    //more properties....

}

public class ClassB
{
    //[PK]
    public string PolicyNumber;

    //[PK]
    public string PolicyDateTime;

    //more properties
}

我想获取 ClassA 的实例并获取所有匹配的 ClassB 行 保单号。我正在尝试进行一对多的事情, 但我意识到这在技术上可能是多对多,我是 只是视为一对多。

我尝试过使用关联类,但还不够深入,无法看到 如果有效的话。我对这些更复杂的映射不熟悉,正在寻找 寻求建议。我对几乎任何想法都持开放态度。

谢谢, 科里

I'm hoping someone can help me with mapping a legacy database. The
problem I'm describing here has plagued others, yet I was unable to
find a real good solution around the web.

DISCLAIMER: this is a legacy DB. I have no control over the composite
keys. They suck and can't be changed no matter much you tell me they
suck. I can't add surrogate keys either. Please don't suggest either of these as they are not options.

I have 2 tables, both with composite keys. One of the keys from one
table is used as part of the key to get a collection from the other
table. In short, the keys don't fully match between the table.
ClassB is used everywhere I would like to avoid adding properties for
the sake of this mapping if possible.

public class ClassA
{
    //[PK]
    public string SsoUid;

    //[PK]
    public string PolicyNumber;

    public IList<ClassB> Others;

    //more properties....

}

public class ClassB
{
    //[PK]
    public string PolicyNumber;

    //[PK]
    public string PolicyDateTime;

    //more properties
}

I want to get an instance of ClassA and get all ClassB rows that match
PolicyNumber. I am trying to get something going with a one-to-many,
but I realize that this may technically be a many-to-many that I am
just treating as one-to-many.

I've tried using an association class but didn't get far enough to see
if it works. I'm new to these more complex mappings and am looking
for advice. I'm open to pretty much any ideas.

Thanks,
Corey

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

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

发布评论

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

评论(2

写给空气的情书 2024-09-01 08:28:16

处理映射旧数据库模式的最简单方法是将代理生成的主键(即 SQL Server 中的标识)添加到每个数据库表,并将现有的复合主键更改为唯一约束。这允许您保留现有的外键并使 NHibernate 映射变得容易。

如果这是不可能的,那么您可以在映射中使用 property-ref 来完成此操作。

编辑:您始终可以退回到贫乏的域模型。也就是说,映射每个类但排除关系。您将拥有一种数据访问方法来通过键获取 ClassA,另一种数据访问方法通过 PolicyNumber 获取 ClassB 的集合。

The easiest way to handle mapping legacy database schemas is to add a surrogate generated primary key (i.e. identity in SQL Server) to each database table and change your existing composite primary keys to unique constraints. This allows you to keep your existing foreign keys and makes the NHibernate mapping easy.

If that's not possible then you may be able to use property-ref in your mappings to accomplish this.

Edit: You can always fall back to an anemic domain model. That is, map each class but exclude the relationships. You would have one data access method to get ClassA by key, and one to get a collection of ClassB by PolicyNumber.

我很坚强 2024-09-01 08:28:16

我最终让数据库团队同意向我正在处理的表中添加一些代理键。 这是我用来辩护的文件

I was eventually able to get the DB team to concede to adding some surrogate keys to the tables I was dealing with. This is the document I used to plead my case.

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