Nhibernate ComposedId on a staging table

I want to have a staging table with two foreign keys (like ComposedId). But NHibernate automatically creates an id property.

I have the following classes

public class Lace { public virtual int Id { get; set; } public virtual string Hostname { get; set; } public virtual IList<LaceHasCard> LaceHasCards { get; set; } } public class Card { public virtual int Id { get; set; } public virtual string Name { get; set; } public virtual IList<LaceHasCard> LaceHasCards { get; set; } } 

and this manually created staging table

 public class LaceHasCard { public virtual Card Card { get; set; } public virtual Lace Lace { get; set; } } 

<strong> Display

 public LaceMapping() { Id(x => x.Id, map => map.Generator(Generators.Native)); Property(x => x.Hostname); Bag(x => x.LaceHasCards, col => { col.Key(k => k.Column("LaceId")); col.Inverse(true); }, r => r.OneToMany()); } public CardMapping() { Id(x => x.Id, map => map.Generator(Generators.Native)); Property(x => x.Name); Bag(x => x.LaceHasCards, col => { col.Key(k => k.Column("CardId")); col.Inverse(true); }, r => r.OneToMany()); } 

intermediate table display

 public LaceHasCardMapping() { //ComposedId(map => //{ // map.Property(x => x.Card.Id, a => // { // a.Column("CardId"); // }); // map.Property(x => x.Lace.Id, a => // { // a.Column("LaceId"); // }); //}); ManyToOne(x => x.Card, map => { map.Column("CardId"); }); ManyToOne(x => x.Lace, map => { map.Column("LaceId"); }); } 

If I create a schema with ComposedId compilation, NHibernate will create the id property in the table.

 CREATE TABLE [dbo].[LaceHasCard] ( [id] INT NOT NULL, [CardId] INT NULL, [LaceId] INT NULL, PRIMARY KEY CLUSTERED ([id] ASC), CONSTRAINT [FKDC6D54711CD160AE] FOREIGN KEY ([CardId]) REFERENCES [dbo].[Card] ([Id]), CONSTRAINT [FKDC6D547151F8AF85] FOREIGN KEY ([LaceId]) REFERENCES [dbo].[Lace] ([Id]) ); 

If I try to create a schema with ComposedId, I get the following error message:

Unable to instantiate mapping class (see InnerException): EmpLaceMgmt.Models.Mappings.LaceHasCardMapping

What would be the proper way to tell NHibernate to create a compiled identifier?

+1
source share
1 answer

Let me give you a suggestion, just my point - do not use a composite identifier. Use the standard primary key in the database and its C # / entity representation as Id { get; set; } Id { get; set; }

Chapter 24. Best Practices

...

Declare identifier properties in constant classes.

NHibernate makes identifier properties optional. There are all sorts of reasons why you should use them. We recommend that identifiers be "synthetic" (generated without a business value) and not a primitive type. For maximum flexibility, use Int64 or String.

Learn more about synthetic, surrogate keys on the wiki.

In my experience, we should not worry about creating such an object:

 public class LaceHasCard { public virtual int Id { get; set; } // the key public virtual Card Card { get; set; } public virtual Lace Lace { get; set; } } 

Because later it would become so easy to access it:

 session.Get<LaceHasCard>(id) 

And also use it in Subqueries (to filter cards with lace and vice versa)

A single column in a database that is auto-generated should not have any additional bad consequences. But processing such a table is a little (much) simpler ...

So, a summary, my suggestion would be to make all first-level entities citizens with full rights (including a synthetic / surrogate key)

+1
source

All Articles