NHibernate 在一张表上有两个“主”节点键

发布于 2024-11-18 06:55:11 字数 395 浏览 4 评论 0原文

我正在学习 NHibernate,以便将其分层到一个相当特殊的遗留数据库上。其他应用程序使用相同的实时数据库,因此我无法进行会影响它们的更改。

我遇到了一个问题,因为一个代表硬件设备的表有两列用作事实上的主键。一种是真正的主键,即自动生成的行 ID。另一个是唯一且非空的硬件序列号。

数据库中的许多其他表都与该表有外键关系。但是,其中一些使用真正的主键(整数行 id)作为外键,有些则使用设备的硬件 id。

请注意,实际上,硬件 ID 和行 ID 一旦配对,将保持配对状态。

我是否能够在 NHibernate 中创建映射来处理这个问题,或者我是否需要创建一些视图来为我提供更标准化的模式,并使用 INSTEAD OF 触发器来使它们可更新?

使用的数据库是 MSSQL 2000,以防产生影响。

I'm learning NHibernate in order to layer it over a rather peculiar legacy database. Other applications use the same live database, so I can't make changes that will affect them.

I've run into a problem because one table, which represents hardware devices, has two columns that are used as de facto primary keys. One is the real primary key, an auto-generated row id. The other is a unique and non-null hardware serial number.

Many other tables in the database have a foreign-key relationship with this table. However, some of them use the real primary key - the integer row id - as a foreign key, and some use the hardware id of the device instead.

Note that in practice a hardware ID and row ID, once paired, will remain paired.

Will I be able to create mappings to deal with this in NHibernate, or will I need to create some views to give me a more standardized schema, and use INSTEAD OF triggers to make them updatable?

The DB in use is MSSQL 2000, in case this makes a difference.

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

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

发布评论

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

评论(1

倦话 2024-11-25 06:55:11

在您的情况下,我将执行以下操作:

public class HardwareDevice{
    public virtual int Id {get; set;}
    public virtual string SerialNumber {get; set;}
    //Other stuff
}

public class DomainThingA {
    public virtual int Id {get; set;}
    public virtual HardwareDevice Device {get; set;}
    //Other stuff
}

public class DomainThingB {
    public virtual int Id {get; set;}
    public virtual HardwareDevice Device {get; set;}
    //Other stuff
}

使用自动生成的 Id 作为主键来映射您的 HardwareDevice 类。
我的示例使用 FluentNhibernate 作为类映射。

public class HardwareDeviceMap : ClassMap<HardwareDevice> {
    public HardwareDeviceMap(){
        Id(x=>x.Id).GeneratedBy.Native().Column("Id"); //Uses auto number
        Map(x=>x.SerialNumber).Column("SerialNumber");
        //Other mappings
    }
}

现在映射其他两个类:

public class DomainThingAMap : ClassMap<DomainThingA> {
    public DomainThingAMap(){
        Id(x=>x.Id).GeneratedBy.Native(); //Uses auto number
        References(x=>x.Device)
          .Column("DeviceId"); //Joins on Id in HardwareDevice Table by default
        //Other mappings
    }
}

public class DomainThingBMap : ClassMap<DomainThingB> {
    public DomainThingBMap(){
        Id(x=>x.Id).GeneratedBy.Native(); //Uses auto number
        References(x=>x.Device)
           .Column("SerialNumber") //Column in DomainThingB Table
           .PropertyRef("SerialNumber"); //Joins using SerialNumber column (hardware device table)
        //Other mappings
    }
}

类映射的 Property-Ref 功能允许您连接不是这些类型的遗留数据库用途的主键的列。

In your situation I would do the following:

public class HardwareDevice{
    public virtual int Id {get; set;}
    public virtual string SerialNumber {get; set;}
    //Other stuff
}

public class DomainThingA {
    public virtual int Id {get; set;}
    public virtual HardwareDevice Device {get; set;}
    //Other stuff
}

public class DomainThingB {
    public virtual int Id {get; set;}
    public virtual HardwareDevice Device {get; set;}
    //Other stuff
}

Map out your HardwareDevice class using the AutoGenerated Id as the primary key.
My examples use FluentNhibernate for the class maps.

public class HardwareDeviceMap : ClassMap<HardwareDevice> {
    public HardwareDeviceMap(){
        Id(x=>x.Id).GeneratedBy.Native().Column("Id"); //Uses auto number
        Map(x=>x.SerialNumber).Column("SerialNumber");
        //Other mappings
    }
}

Now for mapping out the other two classes:

public class DomainThingAMap : ClassMap<DomainThingA> {
    public DomainThingAMap(){
        Id(x=>x.Id).GeneratedBy.Native(); //Uses auto number
        References(x=>x.Device)
          .Column("DeviceId"); //Joins on Id in HardwareDevice Table by default
        //Other mappings
    }
}

public class DomainThingBMap : ClassMap<DomainThingB> {
    public DomainThingBMap(){
        Id(x=>x.Id).GeneratedBy.Native(); //Uses auto number
        References(x=>x.Device)
           .Column("SerialNumber") //Column in DomainThingB Table
           .PropertyRef("SerialNumber"); //Joins using SerialNumber column (hardware device table)
        //Other mappings
    }
}

The Property-Ref feature of the class maps allows you to join on columns which are not the primary key for these types of legacy database purposes.

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