Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...