Note that there are some explanatory texts on larger screens.

plurals
  1. POFluent NHibernate mapping to return description from lookup table
    primarykey
    data
    text
    <p>We have the following database structure:</p> <pre><code>UserTeam table Id (PK, int not null) UserId (FK, int, not null) TeamId (FK, int, not null) RoleId (FK, int, not null) libRole table Id (PK, int not null) Description (varchar(255), not null) </code></pre> <p>And we have an entity as follows:</p> <pre><code>public class UserTeam { public int Id { get; set; } public Team Team { get; set; } public User User { get; set; } public int RoleId { get; set; } public string Role { get; set; } } </code></pre> <p>We are using Fluent NHibernate and configuring NHibernate automatically (ie, using Automapping classes with overrides).</p> <p>We are trying to get JUST the description column from the libRole table into the "Role" property on the UserTeam table, but really struggling. The following is the closest we have got:</p> <pre><code>public class UserTeamMap : IAutoMappingOverride&lt;UserTeam&gt; { public void Override( FluentNHibernate.Automapping.AutoMapping&lt;UserTeam&gt; mapping ) { mapping.References( m =&gt; m.User ).Column( "UserId" ); mapping.References( m =&gt; m.Team ).Column( "TeamId" ); mapping.Join("Role", join =&gt; { join.Fetch.Join(); join.KeyColumn( "Id" ); join.Map( x =&gt; x.Role, "Description" ); } ); } } </code></pre> <p>Which generates the following SQL:</p> <pre><code>SELECT TOP (@p0) this_.Id as Id70_0_, this_.RoleId as RoleId70_0_, this_.TeamId as TeamId70_0_, this_.UserId as UserId70_0_, this_1_.Description as Descript2_71_0_ FROM [UserTeam] this_ inner join libRole this_1_ on this_.Id=this_1_.Id; </code></pre> <p>Close, but NHibernate is using the Id column on both the UserTeam table and the libRole table in the join, when it should be doing on <code>this_.RoleId=this_1_.Id</code> </p> <p>What are we missing? We don't really want to create a "libRole" entity within the application, as all we really care about is the description values - which are user configurable, so we can't just use an enum either. Can anyone help?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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