Note that there are some explanatory texts on larger screens.

plurals
  1. POFluent NHibernate: Custom ForeignKeyConvention not working with explicitly specified table names
    text
    copied!<p>EDIT: for the tl;dr crowd, my question is: How do I access the mappings from inside the ForeignKeyConvention in order to determine the table name that a given type is mapped to?</p> <p>The long version:</p> <p>I am using Fluent NHibernate to configure NHibernate, and I have a custom foreign key convention that is failing when I alias tables and columns. </p> <p>My tables use a convention where the primary key is always called "PK", and the foreign key is "FK" followed by the name of the foreign key table, e.g., "FKParent". For example:</p> <pre><code>CREATE TABLE OrderHeader ( PK INT IDENTITY(1,1) NOT NULL, ... ) CREATE TABLE OrderDetail ( PK INT IDENTITY(1,1) NOT NULL, FKOrderHeader INT NOT NULL, ... ) </code></pre> <p>To make this work, I've built a custom ForeignKeyConvention that looks like this:</p> <pre><code>public class AmberForeignKeyConvention : ForeignKeyConvention { protected override string GetKeyName( Member member, Type type ) { if ( member == null ) return "FK" + type.Name; // many-to-many, one-to-many, join return "FK" + member.Name; // many-to-one } } </code></pre> <p>This works so long as my entities are named the same as the table. But it breaks when they aren't. For example, if I want to map the OrderDetail table to a class called Detail, I can do so like this:</p> <pre><code>public class DetailMap : ClassMap&lt;Detail&gt; { public DetailMap() { Table( "OrderDetail" ); Id( o =&gt; o.PK ); References( o =&gt; o.Order, "FKOrderHeader" ); ... } } </code></pre> <p>The mapping works for loading a single entity, but when I try to run any kind of complicated query with a join, it fails, because the AmberForeignKeyConvention class is making incorrect assumptions about how the columns are mapped. I.e., it assumes that the foreign key should be "FK" + type.Name, which in this case is Order, so it calls the foreign key "FKOrder" instead of "FKOrderHeader".</p> <p>So as I said above: My question is, how do I access the mappings from inside the ForeignKeyConvention in order to determine a given type's mapped table name (and for that matter, their mapped column names, too)? The answer to <a href="https://stackoverflow.com/questions/2292051/can-i-make-a-fluent-nhibernate-foreign-key-convention-which-includes-parent-key">this question</a> seems to hint at the right direction, but I don't understand how the classes involved work together. When I look through the documentation, it's frightfully sparse for the classes I've looked up (such as the <a href="http://fluentnhibernate.org/api/FluentNHibernate.MappingModel.Identity/IdMapping.htm" rel="nofollow noreferrer">IdMapping class</a>).</p>
 

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