流畅的 NHibernate 连接属性值
我正在尝试加入一个表来检索并设置 POCO 上的属性。以下是场景...
*注意 - 一个应用程序可以属于多个用户会话。
UserSession (Table)
UserSessionId PK
ApplicationId FK
UserName
Application (Table)
ApplicationId PK
Name
UserSession (Poco)
string UserName get;
string ApplicationName get;
我尝试过这样的连接:
Join("Application", j => j.Inverse().KeyColumn("ApplicationId").Map(x => x.ApplicationName));
但是,这个使用 UserSessionTable 的主列作为连接列。查询的部分如下所示:
/**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 配置nhibernate 流畅地使用左表(UserSession)中的正确连接列?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
乔不幸的是我认为你不能指定此栏。我相信这是 nhibernate 的限制(不是 FluentNH)。对此有一些解决方法。这是一篇具有相同类型问题的文章:
Fluent映射中的 NHibernate 连接表不使用主键
编辑:
下面使用新实体的示例:
References(x => x.Application, "ApplicationId")
这允许您指定要加入到应用程序表的列。
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.
如果您设法映射此 UserSession 类,则会产生副作用。考虑以下代码:
您可能不想更改代码中的应用程序名称。然而,ORM 需要始终在两个方向上同步数据才有意义。
您可以通过将其实际映射到数据库中来解决问题,例如使应用程序成为一个真实的实体。
如果此结构对于您的情况来说太复杂,请考虑编写一个根据需要返回数据的查询:
您还可以从查询创建一个新结构(
select new ...
)。另请查看命名查询。If you would manage to map this UserSession class, it would have side effects. Consider this code:
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.
If this structure is too complicated for your case, consider to write a query which returns the data as you need:
You could also create a new structure from the query (
select new ...
). Also take a look at named queries.