Identification structure

I learned how to use free nhibernate, and it's hard for me to figure out how to create my mappings. I have a table with a multi-column primary key, but I cannot display it correctly. I have the following smooth display -

public class MyEntityMappingOverride : IAutoMappingOverride<MyEntity>
    {
        public void Override(AutoMapping<MyEntity> mapping)
        {
            mapping.CompositeId()
                .KeyProperty(x => x.Id_1, "Id_1")
                .KeyProperty(x => x.Id_2, "Id_2");

            mapping.References(x => x.otherEntity)
                .Column("JoinColumn");

            // Commented out to attempt to map to another entity on multiple columns
            //mapping.HasMany(x => x.thirdEntit)
            //    .KeyColumns.Add("thirdId_1", "thirdId_2")
            //    .Cascade.All();



        }
    }

The problem I ran into is the composite identifier doesn't seem to work.

Here is a snippet from the generated mapping file -

<id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Id" />
      <generator class="identity" />
    </id>

Then I get an error that the column (id) was not found in any query table.

Am I really mistaken that my mapping code will give the correct composite identifier? Am I missing something or doing something wrong?

+3
source share
1 answer

( ), CompositeKeys, ID :

NHibernate

...

[Serializable]
public MyEntityIdentifier
{
   public virtual TYPE Id_1;
   public virtual TYPE Id_2;

   // You need to override Equals(MyEntityIdentifier), Equals(object obj) and GetHashCode()
}

public class MyEntityMappingOverride : IAutoMappingOverride<MyEntity>
{
        public void Override(AutoMapping<MyEntity> mapping)
        {
            mapping.CompositeId()
                .ComponentCompositeIdentifier(x => x.MyEntityIdentifier)
                .KeyProperty(x => x.MyEntityIdentifier.Id_1, Id_1, "Id_1")
                .KeyProperty(x => x.MyEntityIdentifier.Id_2, "Id_2");

            // snip
        }
}

(Id_1 Id_2) MyEntityIdentifier.

MyEntityIdentifier, .

MyEntity entity = Session.Load<MyEntity>(new MyEntityIdentifier { Id_1 = Whatever, Id_2 = Whatever });

, , CompositeKeys. NHibernate , .

+3

All Articles