NHibernate 关系映射中的 Formula

简介:

在 Nhibernate 的实体类映射中, 如果实体类的属性需要通过 SQL 计算才能得到, 则可以使用 Formula 选项解决。

Nhibernate 对 Formula 的要求如下:

formula (optional): an SQL expression that defines the value for a computed property. Computed properties do not have a column mapping of their own.

场景1: 映射需要计算的属性

以下面的 Category 映射为例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<? xml  version="1.0" encoding="utf-8" ?>
< hibernate-mapping  xmlns="urn:nhibernate-mapping-2.2" assembly="Northwind" namespace="Northwind">
    < class  name="Category" table="[Categories]" schema="[dbo]">
       < id  name="CategoryID" column="[CategoryID]" type="int" />
       < property  name="CategoryName" column="[CategoryName]" type="string"/>
       < property  name="Description" column="[Description]" type="string"/>
       < property  name="Picture" column="[Picture]" type="binary"/>
 
       < set  name="Products" lazy="true">
          < key  column="CategoryID" />
          < one-to-many  class="Product" not-found="ignore"/>
       </ set >
 
    </ class >
</ hibernate-mapping >

如果要增加一个属性 NameAndDesc , 把 CategoryName 和 Description 两个字段连接起来, 用 Formula 可以这样做:

1
< property  name="NameAndDesc" formula="[CategoryName] + ' ' + [Description]" type="string" />

场景2: 映射复杂的 SQL 类型

SQL 2008 支持空间数据类型 geography 和 geometry , 映射空间数据类型可以通过 Nhibernate 的空间扩展解决, 操作起来比较麻烦, 在客户端不需要空间数据类型或者不能处理空间数据类型的情况下, 可以用 Formula 处理。 示例表结构定义如下:

1
2
3
4
CREATE  TABLE  SpatialTable (
    id int  IDENTITY (1,1),
    GeogCol1 geography,
);

使用 Formula 的映射文件如下:

1
2
3
4
5
6
7
<? xml  version="1.0" encoding="utf-8" ?>
< hibernate-mapping  xmlns="urn:nhibernate-mapping-2.2" assembly="Northwind" namespace="Northwind">
    < class  name="Category" table="[Categories]" schema="[dbo]">
       < id  name="Id" column="[id]" type="int" />
       < property  name="GeoCol1" formula="[GeogCol1].STAsText()" type="string" />
    </ class >
</ hibernate-mapping >

张志敏所有文章遵循创作共用版权协议,要求署名、非商业 、保持一致。在满足创作共用版权协议的基础上可以转载,但请以超链接形式注明出处。

本博客已经迁移到 GitHub , 围观地址: http://beginor.github.io/

本文转自张志敏博客园博客,原文链接:http://www.cnblogs.com/beginor/archive/2012/06/20/2556260.html ,如需转载请自行联系原作者
相关文章
|
1月前
|
SQL 存储 数据库
|
数据库
Entity Framework 一对一关系映射
Entity Framework 一对一关系映射
255 0
Entity Framework 一对一关系映射
|
数据库
Entity Framework 一对多关系映射
Entity Framework 一对多关系映射
112 0
Entity Framework 一对多关系映射
|
数据库 C#
Entity Framework 简单属性映射
Entity Framework 简单属性映射
1087 0
Entity Framework 简单属性映射
|
SQL C# 数据库
Entity Framework 继承映射
Entity Framework 继承映射
177 0
Entity Framework 继承映射