ASP.NET MVC5+EF6搭建三层实例

一、创建项目解决方案

image

1.model层、BLL层、Dal层、Common层,都是类库

image

2.UI层使用MVC5

image

二、使用EF链接数据库

1.创建实体数据模型

image

2.选择来自数据库EF设计器

image

3.创建数据库链接

image

4.选是,点击下一步,选择实体框架6.x,下一步选择要创建的表

image

image

image

三、创建Dal层中链接数据库操作并实现查询父类与子类

1.在Dal层创建一个BaseDBContext类,用来自定义EF上下文,首先引用EF框架。备注:Dal层需要应用model层。

image

BaseDBContext类:

  1 namespace Wchl.EFDal
  2 {
  3     /// <summary>
  4     /// 自定义的EF上下文容器类
  5     /// </summary>
  6     public class BaseDBContext: DbContext
  7     {
  8         /// <summary>
  9         /// 负责根据指定的数据库链接字符串,初始化EF
 10         /// </summary>
 11         public BaseDBContext() : base("name=DonationEntities") { }
 12     }
 13 }
View Code

2.创建BaseDal类来存放每个类中共有的方法:查询、新增、编辑、删除、执行Sql语句。

2.1这里先实现一个查询的方法,然后接下来在扩充

BaseDal类:

  1 namespace Wchl.EFDal
  2 {
  3     /// <summary>
  4     /// 负责操作数据库中的所有表的普通增,删,查,改操作的
  5     /// </summary>
  6     /// <typeparam name="TEntity"></typeparam>
  7     public class BaseDal<TEntity> where TEntity:class
  8     {
  9         //1.0实例化EF上下文容器对象
 10         BaseDBContext db = new BaseDBContext();
 11 
 12         DbSet<TEntity> _dbset;
 13 
 14         public BaseDal()
 15         {
 16             //初始化
 17             _dbset = db.Set<TEntity>();
 18         }
 19 
 20         #region 查询
 21         public List<TEntity> Query(Expression<Func<TEntity, bool>> where)
 22         {
 23             return _dbset.Where(where).ToList();
 24         }
 25         #endregion
 26 
 27     }
 28 }
View Code

2.2创建CompanyDal类和DonationDetailDal来继承BaseDal

CompanyDal类:

  1 namespace Wchl.EFDal
  2 {
  3      public class CompanyDal:BaseDal<Company>
  4     {
  5     }
  6 }
View Code

DonationDetailDal类:

  1 namespace Wchl.EFDal
  2 {
  3     public class DonationDetailDal:BaseDal<DonationDetail>
  4     {
  5         //可以各自类的独有的方法
  6     }
  7 }
View Code

四、在BLL层来调用Dal层 需要应用model层

4.1创建BaseBLL类做为基类,并且调用dal中查询的方法

  1 namespace Wchl.EFBLL
  2 {
  3     public class BaseBLL<TEntity> where TEntity:class
  4     {
  5         //初始化BaseDal泛型类的对象
  6         BaseDal<TEntity> bdal = new BaseDal<TEntity>();
  7 
  8         public List<TEntity> Query(Expression<Func<TEntity, bool>> where)
  9         {
 10             return bdal.Query(where);
 11         }
 12     }
 13 }
View Code

4.2创建CompanyBLL类和DonationDetailBLL类来几层父类

DonationDetailBLL类:

  1 namespace Wchl.EFBLL
  2 {
  3     public class DonationDetailBLL:BaseBLL<DonationDetail>
  4     {
  5     }
  6 }
View Code

CompanyBLL类:

  1 namespace Wchl.EFBLL
  2 {
  3     public class CompanyBLL:BaseBLL<Company>
  4     {
  5     }
  6 }
View Code

五、UI层调用bl层l和model层,在home控制器下创建一个test方法 创建视图Views

5.1 创建Test方法

  1         public ActionResult Test()
  2         {
  3             CompanyBLL cbll = new CompanyBLL();
  4 
  5             return View(cbll.Query(c=>true));
  6         }
View Code

5.2在创建视图的时候使用布局页,并选择list模板,模型类选择Company (Wchl.EFModel)

image

5.3 然后在model层中的App.Config文件中把数据库链接,拷贝到UI层Web.config中

image

image

5.4运行http://localhost:15725/home/test页面,数据就会显示了

image

 

六、完善DAL层中的BaseDal类 实现关联查询、删除、编辑、新增、统一保存的方法

BaseDal类:

  1 namespace Wchl.EFDal
  2 {
  3     /// <summary>
  4     /// 负责操作数据库中的所有表的普通增,删,查,改操作的
  5     /// </summary>
  6     /// <typeparam name="TEntity"></typeparam>
  7     public class BaseDal<TEntity> where TEntity:class
  8     {
  9         //1.0实例化EF上下文容器对象
 10         BaseDBContext db = new BaseDBContext();
 11 
 12         DbSet<TEntity> _dbset;
 13 
 14         public BaseDal()
 15         {
 16             //初始化
 17             _dbset = db.Set<TEntity>();
 18         }
 19 
 20         #region 查询
 21         public List<TEntity> Query(Expression<Func<TEntity, bool>> where)
 22         {
 23             return _dbset.Where(where).ToList();
 24         }
 25 
 26         public List<TEntity> QueryJoin(Expression<Func<TEntity, bool>> where,string[] tableNames)
 27         {
 28             //将子类_dbset 赋值给父类的query
 29             DbQuery<TEntity> query = _dbset;
 30 
 31             foreach (var item in tableNames)
 32             {
 33                 //遍历要连表的表名称,最终得到所有连表以后的DbQuery对象
 34                 query = query.Include(item);
 35             }
 36             return query.Where(where).ToList();
 37         }
 38         #endregion
 39 
 40         #region 新增
 41         public void Add(TEntity model)
 42         {
 43             _dbset.Add(model);
 44         }
 45         #endregion
 46 
 47         #region 编辑
 48         public void Edit(TEntity model,string[] propertyName)
 49         {
 50             if (model == null)
 51             {
 52                 throw new Exception("model必须为实体的对象");
 53             }
 54             if (propertyName == null || propertyName.Any() == false)
 55             {
 56                 throw new Exception("必须至少指定一个要修改的属性");
 57             }
 58 
 59             //将model追加到EF容器
 60             DbEntityEntry entry = db.Entry(model);
 61 
 62             entry.State = EntityState.Unchanged;
 63 
 64             foreach (var item in propertyName)
 65             {
 66                 entry.Property(item).IsModified = true;
 67             }
 68         }
 69         #endregion
 70 
 71         #region 物理删除
 72         //EntityState.Unchanged
 73         public void Delete(TEntity model, bool isAddedEFContext)
 74         {
 75             if (isAddedEFContext == false)
 76             {
 77                 _dbset.Attach(model);
 78             }
 79             //修改状态为deleted
 80             _dbset.Remove(model);
 81         }
 82         #endregion
 83 
 84         #region 统一执行保存
 85         public int SaveChanges()
 86         {
 87             return db.SaveChanges();
 88         }
 89         #endregion
 90 
 91     }
 92 }
View Code

七、完善BLL层BaseBLL类的方法

BaseBLL类:

  1 namespace Wchl.EFBLL
  2 {
  3     public class BaseBLL<TEntity> where TEntity:class
  4     {
  5         //初始化BaseDal泛型类的对象
  6         BaseDal<TEntity> bdal = new BaseDal<TEntity>();
  7         #region 查询
  8         public List<TEntity> Query(Expression<Func<TEntity, bool>> where)
  9         {
 10             return bdal.Query(where);
 11         }
 12 
 13         public List<TEntity> QueryJoin(Expression<Func<TEntity, bool>> where, string[] tableNames)
 14         {
 15             return bdal.QueryJoin(where, tableNames);
 16         }
 17         #endregion
 18 
 19         #region  新增
 20 
 21         public void Add(TEntity model)
 22         {
 23             bdal.Add(model);
 24         }
 25 
 26         #endregion
 27 
 28         #region  编辑
 29 
 30         /// <summary>
 31         /// 要求:model必须是自己定义的实体,此时没有追加到EF容器中
 32         /// </summary>
 33         /// <param name="model"></param>
 34         public void Edit(TEntity model, string[] propertyName)
 35         {
 36             bdal.Edit(model, propertyName);
 37         }
 38 
 39         #endregion
 40 
 41         #region  物理删除
 42 
 43         /// <summary>
 44         /// model必须是自己定义的,一般是按照主键来删除
 45         /// </summary>
 46         /// <param name="model">要删除的实体对象</param>
 47         /// <param name="isAddedEFContext">true:表示model以及追加到了ef容器,false:未追加</param>
 48         public void Delete(TEntity model, bool isAddedEFContext)
 49         {
 50             bdal.Delete(model, isAddedEFContext);
 51         }
 52 
 53         #endregion
 54 
 55         #region 统一执行sql语句
 56 
 57         public int SaveChanges()
 58         {
 59             return bdal.SaveChanges();
 60         }
 61 
 62         #endregion
 63 
 64     }
 65 }
View Code

八、mvc调用测试

  1   public ActionResult Test()
  2         {
  3             CompanyBLL cbll = new CompanyBLL();
  4 
  5             //新增测试:
  6             Company model = new Company()
  7             {
  8                 CName = "测试1111111"
  9             };
 10             cbll.Add(model);
 11 
 12             //修改
 13             //Company model1 = new Company()
 14             //{
 15             //    cID = 8,
 16             //    CName = "测试22221"
 17             //};
 18 
 19             //cbll.Edit(model1, new string[] { "CName" });
 20 
 21 
 22             //删除
 23             var model2 = cbll.Query(c => c.cID == 8).FirstOrDefault();
 24             cbll.Delete(model2, true);//执行sql语句 打开关闭了一次ado.net链接
 25 
 26             //统一将上面的新增,编辑,删除分别生成insert,update,delete语句一次性发送给数据库执行
 27             cbll.SaveChanges();
 28 
 29 
 30 
 31             return View(cbll.Query(c=>true));
 32         }
View Code
复制代码
  1   public ActionResult Test()
  2         {
  3             CompanyBLL cbll = new CompanyBLL();
  4 
  5             //新增测试:
  6             Company model = new Company()
  7             {
  8                 CName = "测试1111111"
  9             };
 10             cbll.Add(model);
 11 
 12             //修改
 13             //Company model1 = new Company()
 14             //{
 15             //    cID = 8,
 16             //    CName = "测试22221"
 17             //};
 18 
 19             //cbll.Edit(model1, new string[] { "CName" });
 20 
 21 
 22             //删除
 23             var model2 = cbll.Query(c => c.cID == 8).FirstOrDefault();
 24             cbll.Delete(model2, true);//执行sql语句 打开关闭了一次ado.net链接
 25 
 26             //统一将上面的新增,编辑,删除分别生成insert,update,delete语句一次性发送给数据库执行
 27             cbll.SaveChanges();
 28 
 29 
 30 
 31             return View(cbll.Query(c=>true));
 32         }
复制代码
posted @ 2018-04-13 15:07  Mr.石  阅读(3634)  评论(0编辑  收藏  举报