Fluent NHibernate:获取、更新和删除时的自定义条件(WHERE 子句)

发布于 2024-09-12 14:39:21 字数 671 浏览 2 评论 0原文

我有一个包含许多客户信息的表现

ID | employeename | customerId
------------------------------
 1 | employee1    |  188
 2 | employee2    |  188
 3 | employee3    |  177

在我只想获取那些 customerId 为 188 的员工。如何使用 Fluent NHibernate 映射此信息,以便在更新和删除时也会有 WHERE customerId = 188?

当前映射类似于:

Id(x => x.Id);
Map(x => x.Name).Column("employeename");
Map(x => x.CustomerId).Column("customerId");

添加 Where("customerId = 188") 仅会在 SELECT 中生成自定义 where 子句。我需要在 saveorupdate 上执行以下 UPDATE 子句。

UPDATE employees SET employeename="employ" WHERE ID = 2 AND customerId = 188;

I have a table that contains information from many customers

ID | employeename | customerId
------------------------------
 1 | employee1    |  188
 2 | employee2    |  188
 3 | employee3    |  177

Now I would like to get only those employees, whose customerId is 188. How do I map this with Fluent NHibernate so, that on update and delete there would also be WHERE customerId = 188 ?

Current Mapping is something like:

Id(x => x.Id);
Map(x => x.Name).Column("employeename");
Map(x => x.CustomerId).Column("customerId");

Adding Where("customerId = 188") only results custom where clause in SELECT. I would need following UPDATE-clause to happen on saveorupdate.

UPDATE employees SET employeename="employ" WHERE ID = 2 AND customerId = 188;

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

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

发布评论

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

评论(1

¢好甜 2024-09-19 14:39:21

你脑子里的 SQL 思维是错误的。

1) 添加 HasMany( x => x.Employees ).Inverse().AsSet();在您的客户课程中。
2) 添加引用( x=> x.Customer );在您的 Employee 类中。

这称为双向映射。

看这里: http://www.progware .org/Blog/post/NHibernate-inverse3dtrue-and-cascade3dsave-update-demo.aspx

这个想法是您创建对象并为其分配值。之后,如果您有正确的映射文件,NHibernate 就会执行 SQL 语句。不要编写您的 sql 查询,然后强制 NHibernate 生成完全相同的查询。相反,编写映射并查看 SQL NHibernate 生成的内容并尝试优化它。

PS:别忘了添加级联......

You are thinking wrong with this SQL mind in your head.

1) Add HasMany( x => x.Employees ).Inverse().AsSet(); in your Customer class.
2) Add References( x=> x.Customer ); in your Employee class.

This is called bidirectional mapping.

Look here: http://www.progware.org/Blog/post/NHibernate-inverse3dtrue-and-cascade3dsave-update-demo.aspx

The idea is that you create your objects and you assign values to them. After that NHibernate executes SQL statements if you have proper mapping files. Dont write your sql queries and then forcing NHibernate to generate exactly the same ones. Instead write your mappings and see what SQL NHibernate generates and try to optimize it.

PS: Dont forget to add the cascade...

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