Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

c# - Fluent NHibernate join for property value

I'm trying to join a table to retreive and set a property on a POCO. Here's the scenario...

*NOTE - An application can belong to many user sessions.

Model relationship

UserSession (Table)

UserSessionId PK
ApplicationId FK
UserName

Application (Table)

ApplicationId PK
Name

UserSession (Poco)

string UserName get;
string ApplicationName get;

I've tried a join like this:

Join("Application", j => j.Inverse().KeyColumn("ApplicationId").Map(x => x.ApplicationName));

However, this uses the primary column of the UserSessionTable for the join column. The part of the query looks like this:

/**SNIP**/
inner join
 Auditing.UserSession US
     on this_.UserSessionId=US.UserSessionId
left outer join
 Auditing.Application A
     on US.UserSessionId=A.ApplicationId
/**SNIP**/

How can i configure nhibernate fluently to use the correct join column from the left table(UserSession)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Joe unfortunately I don't think you can specify this column. I believe this is an nhibernate limitation (not FluentNH). There are workarounds to this. Here is an article with the same type of question:

Fluent NHibernate join tables in mapping not using primary key

Edit:
Example using your new entity below:

References(x => x.Application, "ApplicationId")

This allows you to specify the column on which you are joining to the Application table.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...