Composite keys on a mapping table runtime error
I have 3 tables:
Entity with EntityID as key
EntityType with EntityType as key
EntityTypeMappings This one is the composite key of EntityID and EntityType
So one Entity can be of more than one type.
My map class for Fluent is as follows:
public EntityToEntityTypeLinkMap() {
Table("EntityToEntityTypeLink");
LazyLoad();
CompositeId().KeyReference(x => x.EntityID).KeyReference(x => x.EntityTypeID);
//References<Entity>(x => x.Entity).Column("EntityID");
//References<EntityType>(x => x.EntityType).Column("EntityTypeID");
Map(x => x.CreatedDate).Column("CreatedDate");
Map(x => x.CreatedBy).Column("CreatedBy");
Map(x => x.UpdatedDate).Column("UpdatedDate");
Map(x => x.UpdatedBy).Column("UpdatedBy");
Map(x => x.Version).Column("Version");
}
When this is run, I get an error that the class can not compile. I have the XML output which is as follows:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" lazy="true" mutable="true" name="CFIP.Converts.NHibernate.EntityToEntityTypeLink, CFIP.Converts.NHibernate, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="EntityToEntityTypeLink">
<composite-id mapped="false" unsaved-value="undefined">
<key-many-to-one name="EntityID" class="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="EntityID" />
</key-many-to-one>
<key-many-to-one name="EntityTypeID" class="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="EntityTypeID" />
</key-many-to-one>
</composite-id>
<property name="CreatedDate" type="System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="CreatedDate" />
</property>
<property name="CreatedBy" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="CreatedBy" />
</property>
<property name="UpdatedDate" type="System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="UpdatedDate" />
</property>
<property name="UpdatedBy" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="UpdatedBy" />
</property>
<property name="Version" type="System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Version" />
</property>
</class>
</hibernate-mapping>
If I comment out the composite key section and put in a standard key statement, everything runs fine. Any ideas/suggestions would be greatly appreciated. Thanks.
Support Staff 2 Posted by James Gregory on 05 Nov, 2010 01:35 PM
What's the error you're getting? It'll give a schema-validation message which is useful in diagnosing the issue.
Also, you don't need to specify the
Column
names when they match your property name.