Skip to content

Instantly share code, notes, and snippets.

@hotgazpacho
Created October 25, 2010 13:53
Show Gist options
  • Select an option

  • Save hotgazpacho/644989 to your computer and use it in GitHub Desktop.

Select an option

Save hotgazpacho/644989 to your computer and use it in GitHub Desktop.
Abstract base class with multiple subclasses. Some subclasses have additional properties in DetailXXX tables. DB lacks FK relationships. Relationships are mapped on "natural" composite keys.
// Component is an abstract base class
// Weld inherits from Component
// Weld has additional properties in DetailWeld table
public class ComponentMap : ClassMap<Component>
{
public ComponentMap()
{
Table("Component");
CompositeId().KeyProperty(x => x.ItemId).KeyProperty(x => x.RevNo);
// Properties mapped here
DiscriminateSubClassesOnColumn("ComponentTypeId", 4);
}
}
public class WeldMap : SubclassMap<Weld>
{
public WeldMap()
{
DiscriminatorValue(8);
Join("DetailWeld",
wd =>
{
wd.KeyColumn("ItemId");
wd.KeyColumn("RevNo");
//More properties mapped here
});
}
}
@hotgazpacho

Copy link
Copy Markdown
Author

With this, I get:

NHibernate.FKUnmatchingColumnsException : Foreign key (FK5BB087E7E43D7E5D:DetailWeld [RevNo])) must have same number of columns as the referenced primary key (ComponentMaster [ItemId, RevNo])

@jagregory

Copy link
Copy Markdown

I assume if you swap the two wd.KeyColumn lines around the exception changes to be ... (...:DetailWeld [ItemId])? That would certainly indicate that the second call is overwriting the first.

Are you using the master version or the 1.1 binary?

@hotgazpacho

Copy link
Copy Markdown
Author

Yes on swapping the order of calls to KeyColumn.
FNH is 1.1 binary (1.1.0.685)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment