3

I got a scenario where a composite Id uniquely identifies an entity. I defined the MSSQL to have a multiple primary key on those fields. In addition I would like an auto-incremented id to be used for referencing a one-to-many relationship. Here's the schema:

public class Character
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual Region Region { get; set; }
    public virtual string Realm { get; set; }
    public virtual IList<CharProgression> Progression { get; set; }
}

public class CharProgression
{
    public virtual int Id { get; set; }
    public virtual Character Character { get; set; }
    public virtual Stage Stage { get; set; }
    public virtual int ProgressionPoints { get; set; }
    public virtual int NumOfSaves { get; set; }
}

public class Stage
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
}

The mappings look like this:

class CharacterMap : ClassMap<Character>
{
    public CharacterMap()
    {
        Table("characters");
        Id(ch => ch.Id, "id").GeneratedBy.Identity().Not.Nullable();
        CompositeId().KeyProperty(ch => ch.Region, "region")
            .KeyProperty(ch => ch.Realm, "realm")
            .KeyProperty(ch => ch.Name, "name");
        HasMany<CharProgression>(ch => ch.Progression).Inverse().Cascade.All();
    }
}

class CharProgressionMap : ClassMap<CharProgression>
{
    public CharProgressionMap()
    {
        Table("char_progression");
        CompositeId().KeyReference(cprog => cprog.Character, "char_id",
                     .KeyReference(cprog => cprog.Stage, "stage_id");
        Id(cprog => cprog.Id, "id").GeneratedBy.Identity().Not.Nullable();
        Map(cprog => cprog.ProgressionPoints, "progression_points");
        Map(cprog => cprog.NumOfSaves, "num_of_saves");
    }
}


public class StageMap : ClassMap<Stage>
{
    public StageMap()
    {
        Table("stages");
        Id(st => st.Id, "id").GeneratedBy.Identity().Not.Nullable();
        Map(st => st.Name, "name");
        Map(st => st.Description, "description");
    }
}

Now, the thing is that I would like to use SaveOrUpdate() on a character and use the composite id for the update, since the character uniqueness is defined by those 3 fields - region, realm, name. However, when I am referencing the Character from CharProgression, I don't want to use the composite Id as I don't want the char_progression table to hold 3 fields for identifying a character, a simple Id is enough... which is why I also defined an IDENTITY id on the Character entity.

Is what i'm trying possible? or is there another way to achieve this?

Thanks :)

Yuval Kalev
  • 111
  • 6

0 Answers0