如何将现有表映射到 NHibernate 中的继承层次结构中?

发布于 2024-12-07 15:47:39 字数 1266 浏览 0 评论 0原文

问题是尝试在给定现有表结构的情况下映射继承。该表还被使用原始 SQL 的遗留应用程序使用,即该表无法删除任何现有架构详细信息,但可以添加更多内容。

现有的表已经被映射,并且本质上有一堆存在以下问题的字段......

现有类

class A
{
    // Id etc
    public virtual Client Client { get; set; }
}

所以具有像这样的表结构的

 table A (
        Id INT IDENTITY NOT NULL,
       Client_id INT null,
       primary key (Id)
    )

现在我想引入一个基类

class Base
{
   // Id etc
    public virtual Client Client { get; set; }
}

,它最终会得到一个像......

   table Base (
       Id INT IDENTITY NOT NULL,
       Client_id INT null,  
       primary key (Id)
    )

这样的 表将 A 更改为

class A : Base
{
   // clients moved to the base...
}

将对表执行类似的操作:-

table A(
           Base_id INT IDENTITY NOT NULL,
           Id INT not null,     // I will need to keep the existing Id field...
           Client_id INT null,  // this Client now conflicts with the Base client
           primary key (Base_id)
        )

我正在使用每个类继承的表。

问题是...

“Client”将位于“Base”的表上,并且位于“A”的现有表上。我希望能够继续使用“A”上的“客户端”,作为一种覆盖。

我该怎么做?我可以这样做吗?

或者是否有可能在表“Base”上根本没有“Client”,然后在所有子类表上定义“Client”? (这会让查询变得有趣)

The problem is trying to map inheritance given an existing table structure. The table is also used by legacy apps using raw sql, ie, the table can't delete any existing schema details, but can add more to it.

The existing table is already mapped, and essentially has a bunch of fields with the following problem....

so existing class

class A
{
    // Id etc
    public virtual Client Client { get; set; }
}

with a table structure like

 table A (
        Id INT IDENTITY NOT NULL,
       Client_id INT null,
       primary key (Id)
    )

Now I want to introduce a base class

class Base
{
   // Id etc
    public virtual Client Client { get; set; }
}

which will end up with a table like...

   table Base (
       Id INT IDENTITY NOT NULL,
       Client_id INT null,  
       primary key (Id)
    )

and change A to

class A : Base
{
   // clients moved to the base...
}

which will do something like this to the table :-

table A(
           Base_id INT IDENTITY NOT NULL,
           Id INT not null,     // I will need to keep the existing Id field...
           Client_id INT null,  // this Client now conflicts with the Base client
           primary key (Base_id)
        )

I'm using table per class inheritance.

the problem is....

'Client' will be on the table of "Base" and also it's on the existing table of "A". I want to be able to keep using the 'Client' on "A", as a kind of override.

How do I do this? can I do this?

or is it possible that on table "Base" it doesn't have 'Client' at all and then on all the subclass tables they define "Client"? (which would make querying interesting)

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

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

发布评论

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

评论(1

愚人国度 2024-12-14 15:47:39

如果只是查询NH应该支持接口查询

class A : IHasClient
{
    // Id etc
    public virtual Client Client { get; set; }
}

class SomeOther : IHasClient
{
    // Id etc
    public virtual Client Client { get; set; }
}


var classesWithClients = session.QueryOver<IHasClient>().List();

if its only for querying NH should support Querying by interface

class A : IHasClient
{
    // Id etc
    public virtual Client Client { get; set; }
}

class SomeOther : IHasClient
{
    // Id etc
    public virtual Client Client { get; set; }
}


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