Raphael Basso
Raphael Basso

Reputation: 137

FluentNHibernate - Map Foreign Key As Primary Key

Hi I'm trying to migrate a database to ORM, and found a problem. 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). 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?

Upvotes: 0

Views: 1018

Answers (1)

Firo
Firo

Reputation: 30803

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");
    }
}

Upvotes: 1

Related Questions