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.
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)?
If you would manage to map this UserSession class, it would have side effects. Consider this code:
// assume that both user sessions share the same Application.
UserSession userSession1 = session.Get<UserSession>(1);
UserSession userSession2 = session.Get<UserSession>(2);
// what should happen here?
userSession1.ApplicationName = "Blah";
You may not want to change the application name in your code. However, the ORM needs to synchronize data always in both directions to make sense.
You could fix the problem by actually map it as it is in the database, say by making the application a real entity.
class UserSession
{
Application Application { get; private set; }
}
If this structure is too complicated for your case, consider to write a query which returns the data as you need:
select session.Name, application.Name
from UserSession session join Application
You could also create a new structure from the query ( select new ...
). Also take a look at named queries.
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.
链接地址: http://www.djcxy.com/p/60988.html上一篇: NHibernate的Linq属性
下一篇: 流利NHibernate加入属性值