Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use composite Ids in one-to-many mappings in fluent nhibernate?
    primarykey
    data
    text
    <p>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:</p> <pre><code>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&lt;CharProgression&gt; 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; } } </code></pre> <p>The mappings look like this:</p> <pre><code>class CharacterMap : ClassMap&lt;Character&gt; { public CharacterMap() { Table("characters"); Id(ch =&gt; ch.Id, "id").GeneratedBy.Identity().Not.Nullable(); CompositeId().KeyProperty(ch =&gt; ch.Region, "region") .KeyProperty(ch =&gt; ch.Realm, "realm") .KeyProperty(ch =&gt; ch.Name, "name"); HasMany&lt;CharProgression&gt;(ch =&gt; ch.Progression).Inverse().Cascade.All(); } } class CharProgressionMap : ClassMap&lt;CharProgression&gt; { public CharProgressionMap() { Table("char_progression"); CompositeId().KeyReference(cprog =&gt; cprog.Character, "char_id", .KeyReference(cprog =&gt; cprog.Stage, "stage_id"); Id(cprog =&gt; cprog.Id, "id").GeneratedBy.Identity().Not.Nullable(); Map(cprog =&gt; cprog.ProgressionPoints, "progression_points"); Map(cprog =&gt; cprog.NumOfSaves, "num_of_saves"); } } public class StageMap : ClassMap&lt;Stage&gt; { public StageMap() { Table("stages"); Id(st =&gt; st.Id, "id").GeneratedBy.Identity().Not.Nullable(); Map(st =&gt; st.Name, "name"); Map(st =&gt; st.Description, "description"); } } </code></pre> <p>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.</p> <p>Is what i'm trying possible? or is there another way to achieve this?</p> <p>Thanks :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload