简体   繁体   English

FluentNHibernate-将外键映射为主键

[英]FluentNHibernate - Map Foreign Key As Primary Key

Hi I'm trying to migrate a database to ORM, and found a problem. 嗨,我正在尝试将数据库迁移到ORM,发现一个问题。 The database was created with the concept of inheritance, where there is a parent table and several child tables who inherit their properties. 该数据库是采用继承的概念创建的,其中有一个父表和几个继承其属性的子表。 To facilitate querys with INNER JOIN, all child tables contained only reference to the identification of the parent table (foreign keys that were at the same time are primary keys). 为了方便使用INNER JOIN进行查询,所有子表仅包含对父表标识的引用(同时是外键是主键)。 Follows the model: 遵循模型:

BaseDocument
{
    Id : Long
    ...
}

AdministrativeDocument
{
    Id : Long (PK), (FK : BaseDocument(Id))
    ...
}

PropositionDocument
{
    Id : Long (PK), (FK : BaseDocument(Id))
    ...
}

ProjectDocument
{
    Id : Long (PK), (FK : BaseDocument(Id))
    ...
}

My question is: is there any way to create a mapping where a foreign key is also a primary key in FluentNHibernate with C#, or will I have to create separate primary keys for each table? 我的问题是:有什么方法可以创建映射,其中外键也是C#在FluentNHibernate中的主键,还是我必须为每个表创建单独的主键?

you have a standard Table per class inheritance hierarchy. 每个类继承层次结构都有一个标准表。 Just map it as that 只需将其映射为

// inherit
class AdministrativeDocument : Document { }

// base class mapping
class DocumentMap : ClassMap<Document>
{
    public DocumentMap()
    {
        Id(x => x.Id, "Id")...;
    }
}

// subclass mapping, same for all three subtables
class AdministrativeDocumentMap : SubclassMap<AdministrativeDocument>
{
    public AdministrativeDocumentMap()
    {
        KeyColumn("Id");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM