博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
EF查询数据库框架的搭建
阅读量:4338 次
发布时间:2019-06-07

本文共 17060 字,大约阅读时间需要 56 分钟。

一个简单的EF查询框架除了运行项目外,大概需要5个类库项目,当然这个不是一定要这样做,这可以根据自己的需要设置有多少个项目。这里介绍的方法步骤只适合EF零基础的人看看就是了。

在开始之前,先建立一个运行项目,不管是MVC还是winfrom项目,只要能运行就OK,这是为了在后面能测试EF框架的地方。

一、模型项目。这个项目主要负责与数据库映射的,里面的类都是数据库表的实体。

首先在解决方案中建一个文件夹EFModel,这里将会收纳所有的与EF查询有关的项目,这样做只是为了将他们与运行项目分开而已,没有什么特别的意思。

在EFModel中建立一个类库,命名为DBModel,然后在这个类库中创建一个EF

添加之后,选择从数据库生成

在这里,服务器名可以输入数据库所在的服务器的ip,然后测试一下链接,看是否成功。在下面的链接到数据库中选择你要链接的数据库

下一步后,如果没有什么特别设置,就直接完成就可以了。然后看看生成的App.config文件,在connectionStrings节点下就是数据库的链接字符串了

由于没有选择任何的表,因此在数据库模型中就是空的,这时需要添加新的模型来映射数据库中的表,打开model.edmx,然后右键:

下面这一步最好选择是,否则在后面运行程序的时候可能会出现:基础提供程序在 Open 上失败。的错误

 

找到要添加的表,然后点完成

最后保存model,就能看到在model.tt下添加了一个实体类。至此,模型项目就完成了,如果以后需要添加新的实体类,也从数据库更新模型就是了。

这种模式下,必须是先在数据库中把表建好,然后才能添加实体类,如果需要对已经添加了实体类的表做结构上的修改,那么在数据库中完成修改后,需要在model.edmx中先删除原来的实体,然后重新添加一次才行。

二、增删改查公用方法的接口项目IDAO

建立一个类库项目EF.IDAO,添加两个引用

然后添加接口IBaseDao,

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace  EF.IDAO{   public interface IBaseDao
where T:class, new ()//约束T类型必须可以实例化 { //根据条件获取实体对象集合 IQueryable
LoadEntites();      //千万别用下面两种方式去查询,因为这两种方式是吧整个表所有的数据查出来后再按条件筛选的   
    //IQueryable
LoadEntites(Func
whereLambda );
    //IQueryable
LoadEntites(Func
whereLambda, int pageIndex, int pageSize,out int totalCount);     //增加     T AddEntity(T entity);     //更新     T UpdateEntity(T entity);     //删除     bool DelEntity(T entity);     //根据条件删除     bool DelEntityByWhere(Func
whereLambda); } }

此时基接口中的CRUD方法就定义完成。接下来我们需要使用T4模版生成所有的实体类接口并实现IBaseDao接口。

添加名为IDaoExt的T4模板

加入如下代码:

<#@ template debug="false" hostspecific="true" language="C#" #><#@ assembly name="System.Core" #><#@ import namespace="System.Linq" #><#@ import namespace="System.Text" #><#@ import namespace="System.Collections.Generic" #><#@ include file="EF.Utility.CS.ttinclude"#><#@ output extension=".cs" #><#CodeGenerationTools code = new CodeGenerationTools(this);MetadataLoader loader = new MetadataLoader(this);CodeRegion region = new CodeRegion(this, 1);MetadataTools ef = new MetadataTools(this);string inputFile = @"..\\DBModel\\Model.edmx";//指定edmx实体模型所在的路径EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);string namespaceName = code.VsNamespaceSuggestion();EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);#>using System;using System.Collections.Generic;using System.Linq;using System.Text;using DBModel;//引用Domain的命名空间namespace EF.IDAO //实体类接口所在的命名空间{<#foreach (EntityType entity in ItemCollection.GetItems
().OrderBy(e => e.Name)) //遍历edmx模型中映射的实体对象{#> public interface I<#=entity.Name#>Dao:IBaseDao<<#=entity.Name#>> //生成实体对象接口,这里生成的接口的名称是根据模型中的实体类名来设置的,而实体的类名是在建数据库表名的时候设置的名称 { }<#};#>}

其中,有注释的地方是需要根据自己的情况修改的地方(后面项目中的文本模板文件也是如此)。保存文件,然后就可以在IDaoExt.tt下生成了一个文件IDaoExt.cs,这个文件中有一个接口,这个接口就是上面添加的实体所对应的接口。它是根据模板IDaoExt.tt来生成的,因此每当添加了新的实体后,都需要重新保存一次这些tt文件。当然,也可以不用这个模板文件来生成对应的接口,那样的话就需要手动的添加了,这根据自己的爱好决定用哪种方式。

在项目中建一个局部接口IDBSession,注意,是局部接口,要用到关键字partial。

namespace EF.IDAO{    public partial interface IDBSession    {        int SaveChange();//用于在业务逻辑层对提交进行管理    }}

这个接口主要是提供实体类接口的访问属性,以实现对数据访问层的封装。建一个文本模板IDBSession.tt

<#@ template debug="false" hostspecific="true" language="C#" #><#@ assembly name="System.Core" #><#@ import namespace="System.Linq" #><#@ import namespace="System.Text" #><#@ import namespace="System.Collections.Generic" #><#@ include file="EF.Utility.CS.ttinclude"#><#@ output extension=".cs" #><#CodeGenerationTools code = new CodeGenerationTools(this);MetadataLoader loader = new MetadataLoader(this);CodeRegion region = new CodeRegion(this, 1);MetadataTools ef = new MetadataTools(this);string inputFile = @"..\\DBModel\\Model.edmx";EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);string namespaceName = code.VsNamespaceSuggestion();EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);#>using System;using System.Collections.Generic;using System.Linq;using System.Text;using DBModel;using EF.IDAO;namespace EF.IDAO{  public partial interface IDBSession  {    <#foreach (EntityType entity in ItemCollection.GetItems
().OrderBy(e => e.Name)) {#> I<#=entity.Name#>Dao <#=entity.Name#>Dao { get; set; } <#};#> }}

保存之后,就自动生成了IDBSession的局部接口了

再添加一个接口IDBSessionFactory,为业务逻辑层提供统一访问入口:

namespace EF.IDAO{   public interface IDBSessionFactory    {        IDBSession GetCurrentDBSession();    }}

该接口中定义GetCurrentDBSession()方法,其作用是通过该接口方法获取需要的实体对象。至此,这个项目基本上完成了,在这里添加了如下文件:

 

三、增删改查等方法的实现项目DAO

建好项目后添加如下引用:

创建ObjectContextFactory类,用来获取EF上下文。当网站访问量增大时,为避免EF产生的脏数据问题,我们使用System.Runtime.Remoting.Messaging 命名空间下的CallContext来解决线程内上下文唯一。

namespace EF.DAO{    public class ObjectContextFactory    {        public static DbContext GetCurrentObjectContext()        {            //从CallContext数据槽中获取EF上下文            DbContext objectContext = CallContext.GetData(typeof(ObjectContextFactory).FullName) as DbContext;            if (objectContext == null)            {                //如果CallContext数据槽中没有EF上下文,则创建EF上下文,并保存到CallContext数据槽中                objectContext = new DBContent();//当数据库替换为MySql等,只要在此处EF更换上下文即可。这里的DBContent是model.context.cs中的局部类                CallContext.SetData(typeof(ObjectContextFactory).FullName, objectContext);            }            return objectContext;        }    }}

创建BaseDao类,实现IBaseDao中定义方法,用于所有实体类继承此基类。但是,这个类和接口IBaseDao没有任何的关系,他们之间的联系体现在这个类的派生类实现了IBaseDao这个接口

namespace EF.DAO{    public class BaseDao
where T : class, new() { DbContext objectContext = ObjectContextFactory.GetCurrentObjectContext() ;//获取EF上下文 ///
/// 加载实体集合 /// ///
///
public virtual IQueryable
LoadEntites() { return objectContext.Set
().AsQueryable
(); }///
/// 添加实体 /// ///
///
返回更新后的实体
public virtual T AddEntity(T entity) { objectContext.Set
().Add(entity); objectContext.SaveChanges(); return entity; } ///
/// 更新实体 /// ///
///
返回更新后的实体
public virtual T UpdateEntity(T entity) { objectContext.Set
().Attach(entity); objectContext.Entry
(entity).State = EntityState.Modified;//将附加的对象状态更改为修改 objectContext.SaveChanges(); return entity; } ///
/// 删除实体 /// ///
///
public virtual bool DelEntity(T entity) { objectContext.Set
().Attach(entity); objectContext.Entry
(entity).State = EntityState.Deleted;//将附加的实体状态更改为删除 if (objectContext.SaveChanges() > 0) { return true;//删除成功 } else { return false;//删除失败 } } ///
/// 根据条件删除对象 /// ///
条件 ///
public virtual bool DelEntityByWhere(Func
whereLambda) { var tmp = objectContext.Set
().Where
(whereLambda);//根据条件从数据库中获取对象集合 foreach (var entity in tmp) { objectContext.Entry
(entity).State = EntityState.Deleted;//标记对象为删除状态删除 } if (objectContext.SaveChanges() > 0) { return true; } else { return false; } } }}

使用T4模板生成所有实体对象的实现,生成所有的实体类继承自BaseDao并实现各自的接口。创建一个模板文件DaoExt

<#@ template debug="false" hostspecific="true" language="C#" #><#@ assembly name="System.Core" #><#@ import namespace="System.Linq" #><#@ import namespace="System.Text" #><#@ import namespace="System.Collections.Generic" #><#@ include file="EF.Utility.CS.ttinclude"#><#@ output extension=".cs" #><#CodeGenerationTools code = new CodeGenerationTools(this);MetadataLoader loader = new MetadataLoader(this);CodeRegion region = new CodeRegion(this, 1);MetadataTools ef = new MetadataTools(this);string inputFile = @"..\\DBModel\\Model.edmx";EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);string namespaceName = code.VsNamespaceSuggestion();EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);#>using System;using System.Collections.Generic;using System.Linq;using System.Text;using DBModel;using EF.IDAO;namespace EF.DAO{<#foreach (EntityType entity in ItemCollection.GetItems
().OrderBy(e => e.Name)){#> public partial class <#=entity.Name#>Dao:BaseDao<<#=entity.Name#>>,I<#=entity.Name#>Dao { }<#};#>}

创建文本模板DBSessionExt,来生成DBSession实现接口IDBSession

<#@ template debug="false" hostspecific="true" language="C#" #><#@ assembly name="System.Core" #><#@ import namespace="System.Linq" #><#@ import namespace="System.Text" #><#@ import namespace="System.Collections.Generic" #><#@ output extension=".cs" #><#@ include file="EF.Utility.CS.ttinclude"#><#CodeGenerationTools code = new CodeGenerationTools(this);MetadataLoader loader = new MetadataLoader(this);CodeRegion region = new CodeRegion(this, 1);MetadataTools ef = new MetadataTools(this);string inputFile = @"..\\DBModel\\Model.edmx";EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);string namespaceName = code.VsNamespaceSuggestion();EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);#>using System;using System.Collections.Generic;using System.Linq;using System.Text;using EF.IDAO;using System.Data.Entity;namespace EF.DAO{  public partial class DBSession : IDBSession  {  private DbContext _efContext;        //EF上下文 这段代码是因为要实现IDBSession接口中的SaveChange方法而写的,因为每次保存这个文本模板时,如果没有这段代码那么在生成的类中就会缺少SaveChange方法的实现,从而导致编译不过        public DbContext EfContext        {            get            {                if (_efContext == null)                {                    _efContext = ObjectContextFactory.GetCurrentObjectContext();                }                return _efContext;            }            set { _efContext = value; }        }        public int SaveChange()        {            return EfContext.SaveChanges();//调用SaveChanges()方法提交操作        }    <#foreach (EntityType entity in ItemCollection.GetItems
().OrderBy(e => e.Name)) {#> private I<#=entity.Name#>Dao _<#=entity.Name#>Dao; public I<#=entity.Name#>Dao <#=entity.Name#>Dao { get { if (_<#=entity.Name#>Dao == null) { _<#=entity.Name#>Dao = new <#=entity.Name#>Dao(); } return _<#=entity.Name#>Dao; } set { _<#=entity.Name#>Dao = value; } } <#}#> }}

添加一个类DBSessionFactory,实现接口IDBSessionFactory

namespace EF.DAO{    public class DBSessionFactory : IDBSessionFactory    {        public IDBSession GetCurrentDBSession()        {            IDBSession dbSession = CallContext.GetData(typeof(DBSessionFactory).FullName) as DBSession;            if (dbSession == null)            {                dbSession = new DBSession();                CallContext.SetData(typeof(DBSessionFactory).FullName, dbSession);            }            return dbSession;        }    }}

至此,我们就已经完成了对数据访问层的封装当数据库中的表或字段有更新时,我们只需要重新运行一下相应T4模版,就可以实现与数据库保存一致。一共添加了如下文件

 

四、业务逻辑层的封装

创建名为EF.IBLL的程序集,主要用于业务逻辑层接口定义

创建接口IBaseService,这个接口和IBaseDao一模一样的,但是这个接口是业务逻辑层的,而IBaseDao是数据访问层的。所以可以将IBaseDao直接拷过来换个名字就行了

创建名为IServiceExt的T4模版,用于自动生成所有实体对象的接口,并继承自IBaseService接口

<#@ template debug="false" hostspecific="true" language="C#" #><#@ assembly name="System.Core" #><#@ import namespace="System.Linq" #><#@ import namespace="System.Text" #><#@ import namespace="System.Collections.Generic" #><#@ output extension=".cs" #><#@ include file="EF.Utility.CS.ttinclude"#><#CodeGenerationTools code = new CodeGenerationTools(this);MetadataLoader loader = new MetadataLoader(this);CodeRegion region = new CodeRegion(this, 1);MetadataTools ef = new MetadataTools(this);string inputFile = @"..\\DBModel\\Model.edmx";EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);string namespaceName = code.VsNamespaceSuggestion();EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);#>using System;using System.Collections.Generic;using System.Linq;using System.Text;using DBModel;namespace EF.IBLL{<#foreach (EntityType entity in ItemCollection.GetItems
().OrderBy(e => e.Name)){#> public interface I<#=entity.Name#>Service : IBaseService<<#=entity.Name#>> { }<#};#>}

至此,业务逻辑层定义完成

五、实现业务逻辑层,对数据访问层的调用

创建项目EF.BLL,创建名为BaseService基类。该基类中实现了对数据访问层的调用,也实现了增删改查

步骤: 1、先将IDBSessionFactory封装为属性,用于获取IDBSession

                     2、再将IDBSession封装为属性,用于获取EF上下文对象

                     3、定义IBaseDao类型的CurrentDao属性,用于属性获取具体的实体对象

                     4、定义抽象方法 SetCurrentDao(),用于子类设置实现,为CurrentDao属性赋具体的实体对象

namespace EF.BLL{    public abstract class BaseService
where T : class, new() { //构造函数 public BaseService() { //调用SetCurrentDao()方法,要求子类必须实现 SetCurrentDao(); } //获取EF实体工厂 IDBSessionFactory _dbSessionFactory; IDBSession _dbSession; public IDBSessionFactory DbSessionFactory { get { if (_dbSessionFactory == null) { _dbSessionFactory = new DBSessionFactory(); } return _dbSessionFactory; } set { _dbSessionFactory = value; } } public IDBSession DbSession { get { if (_dbSession == null) { _dbSession = DbSessionFactory.GetCurrentDBSession();//通过数据访问层提供的工厂获取EF实体对象 } return _dbSession; } set { _dbSession = value; } } //数据访问层基接口类型可以接收数据访问层的所有实体Dao public IBaseDao
CurrentDao { get; set; } //该方法用于子类实现,其作用是设置相应的实体Dao public abstract void SetCurrentDao(); }}

在实现增加和更新方法时,我们这时调用DBSessin中封装的SaveChanges()方法进行提交,主要目的是实现业务层控制提交。由于EF具有延迟加载特性,因此我们利用此特性,实现批量操作时,一次提交数据库,以提高程序性能,因此我们这时需要将BaseDao中的增加和更新方法中的SaveChange()方法注视掉。

基类BaseService剩下的代码:

//以下是CRUD实现        public virtual IQueryable
LoadEntites() { return this.CurrentDao.LoadEntites(); } public virtual T AddEntity(T entity) { var tmp = this.CurrentDao.AddEntity(entity); this.DbSession.SaveChange(); return tmp; } public virtual T UpdateEntity(T entity) { var tmp = this.CurrentDao.UpdateEntity(entity); this.DbSession.SaveChange(); return tmp; } public virtual bool DelEntity(T entity) { return this.CurrentDao.DelEntity(entity); } public virtual bool DelEntityByWhere(Func
whereLambda) { return this.DelEntityByWhere(whereLambda); }

至此,BaseService业务逻辑层基类就封装完成,接下来使用T4模版自动生成所有实体。

创建名为ServiceExt的T4模版

<#@ template debug="false" hostspecific="true" language="C#" #><#@ assembly name="System.Core" #><#@ import namespace="System.Linq" #><#@ import namespace="System.Text" #><#@ import namespace="System.Collections.Generic" #><#@ output extension=".cs" #><#@ include file="EF.Utility.CS.ttinclude"#><#CodeGenerationTools code = new CodeGenerationTools(this);MetadataLoader loader = new MetadataLoader(this);CodeRegion region = new CodeRegion(this, 1);MetadataTools ef = new MetadataTools(this);string inputFile = @"..\\DBModel\\Model.edmx";EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);string namespaceName = code.VsNamespaceSuggestion();EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);#>using System;using System.Collections.Generic;using System.Linq;using System.Text;using EF.IBLL;using DBModel;namespace EF.BLL{<#foreach (EntityType entity in ItemCollection.GetItems
().OrderBy(e => e.Name)){#> public partial class <#=entity.Name#>Service : BaseService<<#=entity.Name#>>, I<#=entity.Name#>Service { public override void SetCurrentDao() { this.CurrentDao = this.DbSession.<#=entity.Name#>Dao; } }<#};#>}

这时我们就对业务逻辑层封装完成。

现在需要建一个运行项目,来测试是否真的可以访问数据库了。项目建好后,添加DBModel,IBLL,BLL的引用。将DBModel中app.config里面的字符串链接复制到运行项目中的web.config中

然后在要访问数据库的方法中通过下面的代码来实现:

EF.IBLL.IN_CommodityService ics = new EF.BLL.N_CommodityService();           IList
lis = ics.LoadEntites().where(t => t.Id_bigint < 200).ToList(); int s = lis.Count; //如果是分页查询,如下:
IList
les = ics.LoadEntites().where(t => t.Id_bigint < 200).Take(30).Skip(20).ToList(); //还可以通过OrderBy,ThenByDescending,ThenBy等方法来实现排序,这些都要在tolist方法之前调用

也就是说,所有对数据库的访问都是通过EF.IBLL中的实体接口来实现的。

总结:整个数据访问的框架就分为业务逻辑层(EF.IBLL,EF.BLL),数据访问层(EF.IDAO,EF.DAO)和实体(DBModel)三个部分。在创建实体的时候,要注意EF版本的选择,目前来说最好是选择5.0版本的,因为EntityFramework的版本为5.0,否则可能会出现意想不到的错误。还有就是在选择数据连接的时候,对于是否在字符串中包括敏感数据,选择是就行了,这也是为了避免出现不可预知的错误。每次更新了实体的时候,都要重新保存一次所有的文本模板文件,以便能自动生成实体类和接口。

以上介绍的一些对数据库的访问的方法都是linq方式的,有时候我们需要通过sql语句来实现对数据库的访问,这种方式在另一篇随笔再详细的介绍。

 

ThenBy

转载于:https://www.cnblogs.com/jin-/p/5681324.html

你可能感兴趣的文章
Microsoft.Jet.OLEDB.4.0”提供程序不支持 ITransactionLocal 接口。本地事务不可用于当前提供程序...
查看>>
oc 代码块的使用
查看>>
转:Eclipse中打开文件所在文件夹的插件及设置
查看>>
Django 之Form
查看>>
开发ProxyServer的时候如何在一台PC上调试
查看>>
C#用于对用户输入数据进行校验的类
查看>>
低速前碰开发
查看>>
python-9-IO编程
查看>>
【GoLang】转载:我为什么放弃Go语言,哈哈
查看>>
【MySQL】MySQL 如何实现 唯一随机数ID
查看>>
【Redis】Redis分布式集群几点说道
查看>>
HDU2819(KB10-E 二分图最大匹配)
查看>>
mysql主从复制、redis基础、持久化和主从复制
查看>>
文档工具GitBook使用
查看>>
两个链表的第一个公共节点
查看>>
知道这20个正则表达式,能让你少写1,000行代码
查看>>
MariaDB 主从同步与热备(14)
查看>>
推荐的 CSS 书写顺序
查看>>
NIO:与 Buffer 一起使用 Channel
查看>>
Android帧缓冲区(Frame Buffer)硬件抽象层(HAL)模块Gralloc的实现原理分析
查看>>