问题描述
我试图加入一个表来检索和设置一个POCO的财产。这里是...$ b
*注意 - 一个应用程序可以属于许多用户会话。
UserSession(Table)
pre $
UserSessionId PK
ApplicationId FK
用户名
$ b $ h
$ b $ p $ Application $
$
UserSession(Poco)
string UserName get;
string ApplicationName get;
我试过了这样的连接:
(Application,j => j.Inverse()。KeyColumn(ApplicationId).Map(x => x.ApplicationName));
但是,这将使用UserSessionTable的主列作为连接列。查询的部分如下所示:
/ ** SNIP ** /
内连接
Auditing.UserSession US
on this_.UserSessionId = US.UserSessionId
left outer join
Auditing.Application A
US.UserSessionId = A.ApplicationId
/ ** SNIP ** /
如何配置nhibernate以便从左表中正确使用连接列UserSession)?
如果你设法映射这个UserSession类,它会产生副作用。考虑这个代码:
//假定两个用户会话共享相同的应用程序。
UserSession userSession1 = session.Get< UserSession>(1);
UserSession userSession2 = session.Get< UserSession>(2);
//这里应该发生什么?
userSession1.ApplicationName =Blah;
您可能不希望更改代码中的应用程序名称。但是,ORM需要始终在两个方向上同步数据才是有意义的。
您可以通过将数据映射到数据库中来解决问题,比如通过应用程序是一个真正的实体。
$ $ p $ $ $ $
Application Application {get;私人设置}
$
如果这个结构对于你来说太复杂了,考虑写一个查询它会根据需要返回数据:
pre $ 从UserSession会话中选择session.Name,application.Name
加入应用程序
您也可以从查询中创建一个新结构( select new ...
)。也请看看命名查询。
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.
这篇关于连接NHibernate加入属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!