Search code examples
c#nhibernatefluent-nhibernate

Fluent NHibernate Mapping One-To-Many in one Table


I've the following Database Table:

CREATE TABLE RestrictedSystemUsages
(
    WorkflowArtifactIdentifier varchar(512) NOT NULL,
    RestrictedSystemUsageId uniqueidentifier NOT NULL
);

I want to map the following Class into it:

public class RestrictedSystemUsages
{
    public virtual string WorkflowArtifactIdentifier { get; set; }
    public virtual List<Guid> RestrictedSystemUsageIds { get; set; }
}

But I've no clue how the fluent mapping is to be done.

I've tried to apply the following mapping but it won't work at all:

        Id(x => x.WorkflowArtifactIdentifier).GeneratedBy.Assigned();

        HasMany(x => x.RestrictedSystemUsageIds)
            .Table("RestrictedSystemUsageForWorkflow")
            .Element("RestrictedSystemUsageId")
            .AsBag();

Can anybody point me to the correct mapping for the Guid-List please?


Solution

  • The following Mapping was all I need:

    Id(x => x.WorkflowArtifactIdentifier).GeneratedBy.Assigned();
    
    HasMany(x => x.RestrictedSystemUsageIds)
        .Element("RestrictedSystemUsageId")
        .KeyColumn("WorkflowArtifactIdentifier")
        .Table("RestrictedSystemUsageForWorkflow")
        .AsSet();
    

    With one flaw, it will add an additional row with an empty RestrictedSystemUsageId column.