数据库为sql server2005,表名order为关键字,开始直接写fromula= (select sum(o.Price) from [Order] as o where o.CustomerId=Id)
运行后程序报错,查看sql发现其会生成如下代码:
(select sum(o.Price) from customer0_.[Order] as o where o.CustomerId=customer0_.Id) as formula0_0_
FROM Customer customer0_ WHERE customer0_.Id=?
即NHibernate会在我们写的【order】前再加个表名。
想到sql server还可以用双引号表示关键字,而 "为xml中的“转义字符,于是试了一下,果然可以,代码如下:
<property name="Total" type="Double" formula="(select sum(o.Price) from "Order" as o where o.CustomerId=Id)"></property>
已测试通过!
问题解决了,但还是不明白为什么用【】不可以,有知道的朋友望告知!