Fluent NHibernate:获取、更新和删除时的自定义条件(WHERE 子句)
我有一个包含许多客户信息的表现
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你脑子里的 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...