NBear学习笔记(一)

使用已有数据库。(之前先设计实体,再自动生成数据库,发现每次重新生成时,数据库测试数据都清空了)。

本文参考Teddy's Knowledge Base的相关文章,这篇文章只演示单表操作.

Step1: 创建数据库NBearDB

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> 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文件

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> < 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文件,内容如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> <? 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 >               //输出语言
  
<!-- <OutputCodeFileEncoding>utf-8</OutputCodeFileEncoding> -->
  
< 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.将会得到下面的代码:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> 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,现在开始使用了。

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> 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工程就可以了。

运行,大功告成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值