使用已有数据库。(之前先设计实体,再自动生成数据库,发现每次重新生成时,数据库测试数据都清空了)。
本文参考Teddy's Knowledge Base的相关文章,这篇文章只演示单表操作.
Step1: 创建数据库NBearDB
CREATE TABLE [ dbo ] . [ User ] (
[ ID ] [ uniqueidentifier ] NOT NULL ,
[ FirstName ] [ nvarchar ] ( 50 ) ,
[ Status ] [ bit ] NULL ,
[ Birthday ] [ datetime ] NULL ,
[ Email ] [ nvarchar ] ( 127 )
) ON [ PRIMARY ]
Step2:创建解决方案和配置
(1)安装下载的Nbear组件下dist\SetupNBearVsPlugin.exe插件(可自动生成实体类)
(2)打开VS2005,新建一个空的解决方案,添加两个C#类库工程EntityDesigns和Entities,删除自动生成的class1.cs.
(3)添加一个website的网站项目。
(4)在Entities下添加Entities.cs, 在网站根目录下添加web.config文件和EntityConfig.xml文件.
(5)修改web.config文件
< configSections >
< section name ="entityConfig" type ="NBear.Common.EntityConfigurationSection, NBear.Common" />
configSections >
< entityConfig >
< includes >
< add key ="Sample Entity Config" value ="~/EntityConfig.xml" />
includes >
entityConfig >
< appSettings />
< connectionStrings >
< add name ="DbName" connectionString ="Server=WDS;Database=NBearDB;Uid=sa;Pwd=sa"
providerName ="NBear.Data.SqlServer.SqlDbProvider" />
connectionStrings >
(6)在EntityDesigns目录下添加EntityDesignToEntityConfig.xml文件,内容如下:
xml version="1.0" encoding="utf-8" ?>
< EntityDesignToEntityConfiguration xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd ="http://www.w3.org/2001/XMLSchema" >
< CompileMode > Debug CompileMode >
< InputDllName > EntityDesigns.dll InputDllName > //实体设计类
< OutputNamespace > Entities OutputNamespace > //实体类
< OutputLanguage > C# OutputLanguage > //输出语言
< EntityCodePath > ..\Entities\Entities.cs EntityCodePath > //实体类文件
< EntityConfigPath > ..\website\EntityConfig.xml EntityConfigPath >
< SqlSync enable ="false" > //更改实体时,是否同步更新数据库,建议设为false,否则将清空现有数据库的数据
< SqlServerFolder > C:\Program Files\Microsoft SQL Server\80\Tools\Binn SqlServerFolder >
< ServerName > WDS ServerName >
< UserID > sa UserID >
< Password > sa Password >
< DatabaseName > NBearDB DatabaseName >
SqlSync >
EntityDesignToEntityConfiguration >
(7)在EntityDesigns目录下添加EntityDesigns.cs
(8)打开下载的Nbear组件下dist\NBear.Tools.DbToEntityDesign.exe程序 ,设置连接字符串,我的是这样:
Server=WDS;Database=NBearDB;Uid=sa;Pwd=sa 点击connect, 选择要生成的表,点击Generate Entities Design.将会得到下面的代码:
using System;
using System.Collections.Generic;
using System.Text;
using NBear.Common.Design;
namespace EntityDesigns
{
public interface User : Entity
{
[PrimaryKey]
Guid ID { get ; set ; }
[SqlType( " nvarchar(50) " )]
string FirstName { get ; set ; }
bool ? Status { get ; set ; }
DateTime ? Birthday { get ; set ; }
[SqlType( " nvarchar(127) " )]
string Email { get ; set ; }
}
}
拷贝上面的代码到EntityDesigns.cs,为EntityDesigns添加NBear.Common.Design引用.
(9)编译EntityDesigns工程,编译成功,刚才添加Entities.cs,EntityConfig.xml文件将被自动填入正确的内容。
(10)为Entities工程引用NBear.Common,然后编译Entities工程。
(11)为WebSite添加Entities项目编译后的Entities.dll.再引用NBear.Data
Step 3 :OK,现在开始使用了。
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using NBear.Common;
using NBear.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
// 取得数据
Gateway gateway = new Gateway( " DbName " );
User[] u = gateway.FindArray < User > ();
GridView1.DataSource = u;
GridView1.DataBind();
}
}
// 添加数据
Gateway gateway = new Gateway( " DbName " );
User d = new User();
d.Email = " jackxfsdfsdf " ;
d.Name = " very good " ;
d.Status = true ;
gateway.Save(d);备注:加入我们把FirstName字段改为Name,那么我们只需要把EntityDesigns.cs里把这句改为
[MappingName("Name")]
string FirstName{ get; set; }重新编译EntityDesign工程就可以了。
运行,大功告成。