流畅的 NHibernate 连接属性值

发布于 2024-11-07 10:38:12 字数 892 浏览 3 评论 0原文

我正在尝试加入一个表来检索并设置 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.

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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睫毛上残留的泪 2024-11-14 10:38:12

乔不幸的是我认为你不能指定此栏。我相信这是 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.

简单气质女生网名 2024-11-14 10:38:12

如果您设法映射此 UserSession 类,则会产生副作用。考虑以下代码:

// 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";

您可能不想更改代码中的应用程序名称。然而,ORM 需要始终在两个方向上同步数据才有意义。

您可以通过将其实际映射到数据库中来解决问题,例如使应用程序成为一个真实的实体。

class UserSession
{
  Application Application { get; private set; }
}

如果此结构对于您的情况来说太复杂,请考虑编写一个根据需要返回数据的查询:

select session.Name, application.Name
from UserSession session join Application

您还可以从查询创建一个新结构(select new ...)。另请查看命名查询。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文