我一直希望NHibernate能够支持像EF一样支持可视化操作,今天去网上搜了一下,发现有一个插件,类似EF的可视化功能。
下载地址:Mindscape.NHibernateModelDesigner_crack.vsix
下载后,运行Mindscape.NHibernateModelDesigner_crack.vsix,进行安装,安装完成之后,在新建项目的时候,就会发现多了如下模版:
用过EF的朋友应该很熟悉,这个界面就跟EF的可视化界面非常相似了。
打开服务器资源管理器,然后连接到Northwind数据库
拖放之后,将会自动生成如下类
我们来看下这个生成的类的源码
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System;
using System.Collections.Generic;
using NHibernate.Cfg;
using NHibernate.Validator.Constraints;
namespace Shop.Domain
{
[System.CodeDom.Compiler.GeneratedCode("NHibernateModelGenerator", "1.0.0.0")]
public partial class Customer
{
[NotNull]
[Length(Max=5)]
public virtual string CustomerId { get; set; }
[NotNull]
[Length(Max=40)]
public virtual string CompanyName { get; set; }
[Length(Max=30)]
public virtual string ContactName { get; set; }
[Length(Max=30)]
public virtual string ContactTitle { get; set; }
[Length(Max=60)]
public virtual string Address { get; set; }
[Length(Max=15)]
public virtual string City { get; set; }
[Length(Max=15)]
public virtual string Region { get; set; }
[Length(Max=10)]
public virtual string PostalCode { get; set; }
[Length(Max=15)]
public virtual string Country { get; set; }
[Length(Max=24)]
public virtual string Phone { get; set; }
[Length(Max=24)]
public virtual string Fax { get; set; }
private IList<Order> _orders = new List<Order>();
public virtual IList<Order> Orders
{
get { return _orders; }
set { _orders = value; }
}
static partial void CustomizeMappingDocument(System.Xml.Linq.XDocument mappingDocument);
internal static System.Xml.Linq.XDocument MappingXml
{
get
{
var mappingDocument = System.Xml.Linq.XDocument.Parse(@"<?xml version='1.0' encoding='utf-8' ?>
<hibernate-mapping xmlns='urn:nhibernate-mapping-2.2'
assembly='" + typeof(Customer).Assembly.GetName().Name + @"'
namespace='Shop.Domain'
>
<class name='Customer'
table='`Customers`'
>
<id name='CustomerId'
column='`CustomerID`'
>
<generator class='identity'>
</generator>
</id>
<property name='CompanyName'
column='`CompanyName`'
/>
<property name='ContactName'
column='`ContactName`'
/>
<property name='ContactTitle'
column='`ContactTitle`'
/>
<property name='Address'
column='`Address`'
/>
<property name='City'
column='`City`'
/>
<property name='Region'
column='`Region`'
/>
<property name='PostalCode'
column='`PostalCode`'
/>
<property name='Country'
column='`Country`'
/>
<property name='Phone'
column='`Phone`'
/>
<property name='Fax'
column='`Fax`'
/>
<bag name='Orders'
inverse='false'
>
<key column='`CustomerID`' />
<one-to-many class='Order' />
</bag>
</class>
</hibernate-mapping>");
CustomizeMappingDocument(mappingDocument);
return mappingDocument;
}
}
}
}
这个类当中包含了生成的model类和xml映射文件
这里我只是简单的介绍存在这样一个插件,这个差件自动生成了我们需要的映射类和配置文件,以及数据库配置文件,以及NHibenate辅助类,有了这些东西,我们就可以很方便的进行开发了,只要Ctrl+C,Ctrl+V。具体的使用方法,大家可以去参考上一篇:Hibernate学习笔记—使用 NHibernate构建一个ASP.NET MVC应用程序