ASP.NET结合MVC+EF T4模板写后台管理系统框架(6)- EF增删改查


上一节创建了实体数据库,这次我们来看看怎么操作这个实体

代码实现

新建一个UserInfoController的控制器:不需要写什么代码,系统自动生成Index方法:
创建IDAL,DAL,IBLL,BLL的代码:

IDAL层-IUserInfoRepository

using BYCMS.Model;
using System.Linq;

namespace BYCMS.IDAL
{
public interface IUserInfoRepository
{
/// <summary>
    /// 获取列表
    /// </summary>
///
<param name="db">数据库上下文</param>
/// <returns>数据列表</returns>
IQueryable<UserInfo> GetList(DBContainer db);
    /// <summary>
        /// 创建一个实体
        /// </summary>
    ///
    <param name="entity">实体</param>
    int Create(UserInfo entity);
    /// <summary>
        /// 删除一个实体
        /// </summary>
    ///
    <param name="entity">主键ID</param>
    int Delete(int id);
    /// <summary>
        /// 修改一个实体
        /// </summary>
    ///
    <param name="entity">实体</param>
    int Edit(UserInfo entity);
    /// <summary>
        /// 获得一个实体
        /// </summary>
    ///
    <param name="id">id</param>
    /// <returns>实体</returns>
    UserInfo GetById(int id);
    /// <summary>
        /// 判断一个实体是否存在
        /// </summary>
    bool IsExist(int id);
    }
    }

DAL层-UserInfoRepository

using BYCMS.IDAL;
using BYCMS.Model;
using System.Data.Entity;
using System.Linq;

namespace BYCMS.DAL
{
public class UserInfoRepository : IUserInfoRepository
{
/// <summary>
    /// 创建一个实体
    /// </summary>
///
<param name="entity">实体</param>
/// <returns></returns>
public int Create(UserInfo entity)
{
using (DBContainer db = new DBContainer())
{
db.Set<UserInfo>().Add(entity);
    return db.SaveChanges();
    }
    }
    /// <summary>
        /// 删除一个实体
        /// </summary>
    ///
    <param name="id">主键ID</param>
    /// <returns></returns>
    public int Delete(int id)
    {
    using (DBContainer db = new DBContainer())
    {
    UserInfo entity = db.UserInfoSet.Find(id);
    db.Set<UserInfo>().Remove(entity);
        return db.SaveChanges();
        }
        }
        /// <summary>
            /// 修改一个实体
            /// </summary>
        ///
        <param name="entity">实体</param>
        /// <returns></returns>
        public int Edit(UserInfo entity)
        {
        using (DBContainer db = new DBContainer())
        {
        db.Set<UserInfo>().Attach(entity);
            db.Entry(entity).State = EntityState.Modified;
            return db.SaveChanges();
            }
            }
            /// <summary>
                /// 获得一个实体
                /// </summary>
            ///
            <param name="id">主键ID</param>
            /// <returns></returns>
            public UserInfo GetById(int id)
            {
            using (DBContainer db = new DBContainer())
            {
            return db.UserInfoSet.Find(id);
            }
            }
            /// <summary>
                /// 获取列表
                /// </summary>
            ///
            <param name="db">
            </param>
            /// <returns></returns>
            public IQueryable<UserInfo> GetList(DBContainer db)
                {
                IQueryable<UserInfo> list = db.UserInfoSet.AsQueryable();
                    return list;
                    }
                    /// <summary>
                        /// 判断一个实体是否存在
                        /// </summary>
                    ///
                    <param name="id">主键ID</param>
                    /// <returns></returns>
                    public bool IsExist(int id)
                    {
                    using (DBContainer db = new DBContainer())
                    {
                    UserInfo entity = GetById(id);
                    if (entity != null)
                    return true;
                    return false;
                    }
                    }
                    }
                    }

IBLL层-IUserInfoBLL

using BYCMS.Model;
    using System.Collections.Generic;
    
    namespace BYCMS.IBLL
    {
    public interface IUserInfoBLL
    {
    /// <summary>
    /// 获取列表
    /// </summary>
    /// <param name="pager">JQgrid分页</param>
    /// <param name="queryStr">搜索条件</param>
    /// <returns>列表</returns>
    List<UserInfo> GetList();
    /// <summary>
    /// 创建一个实体
    /// </summary>
    /// <param name="errors">持久的错误信息</param>
    /// <param name="model">模型</param>
    /// <returns>是否成功</returns>
    bool Create(UserInfo model);
    /// <summary>
    /// 删除一个实体
    /// </summary>
    /// <param name="errors">持久的错误信息</param>
    /// <param name="id">id</param>
    /// <returns>是否成功</returns>
    bool Delete(int id); 
    /// <summary>
    /// 修改一个实体
    /// </summary>
    /// <param name="errors">持久的错误信息</param>
    /// <param name="model">模型</param>
    /// <returns>是否成功</returns>
    bool Edit(UserInfo model);
    /// <summary>
    /// 根据ID获得一个Model实体
    /// </summary>
    /// <param name="id">id</param>
    /// <returns>Model实体</returns>
    UserInfo GetById(int id);
    /// <summary>
    /// 判断是否存在实体
    /// </summary>
    /// <param name="id">主键ID</param>
    /// <returns>是否存在</returns>
    bool IsExist(int id);
    }
    }

BLL层-UserInfoBLL

using BYCMS.Common;
using BYCMS.DAL;
using BYCMS.IBLL;
using BYCMS.IDAL;
using BYCMS.Model;
using System.Collections.Generic;
using System.Linq;

namespace BYCMS.BLL
{
public class UserInfoBLL : IUserInfoBLL
{
DBContainer db = new DBContainer();
IUserInfoRepository rep = new UserInfoRepository();

/// <summary>
    /// 创建一个实体
    /// </summary>
///
<param name="errors">
</param>
///
<param name="model">
</param>
/// <returns></returns>
public bool Create(UserInfo model)
{
if (rep.Create(model) == 1)
{
return true;
}
return false;
}
/// <summary>
    /// 根据id删除实体
    /// </summary>
///
<param name="errors">
</param>
///
<param name="id">
</param>
/// <returns></returns>
public bool Delete(int id)
{
if(rep.Delete(id) > 0)
{
return true;
}
return false;
}
/// <summary>
    /// 编辑实体
    /// </summary>
///
<param name="errors">
</param>
///
<param name="model">
</param>
/// <returns></returns>
public bool Edit(UserInfo model)
{
if (rep.Edit(model) > 0)
{
return true;
}
return false;
}
/// <summary>
    /// 根据id查找实体
    /// </summary>
///
<param name="id">
</param>
/// <returns></returns>
public UserInfo GetById(int id)
{
return rep.GetById(id);
}
/// <summary>
    /// 实体列表
    /// </summary>
///
<param name="pager">
</param>
///
<param name="queryStr">
</param>
/// <returns></returns>
public List<UserInfo> GetList()
    {
    IQueryable<UserInfo> queryData = rep.GetList(db);
        return queryData.ToList();
        }
        /// <summary>
            /// 判断是否存在实体
            /// </summary>
        ///
        <param name="id">
        </param>
        /// <returns></returns>
        public bool IsExist(int id)
        {
        return rep.IsExist(id);
        }
        }
        }

最后我们往数据库插入几条记录这时你应该看到效果了

结束

声明:⎛⎝韩小韩博客⎠⎞|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - ASP.NET结合MVC+EF T4模板写后台管理系统框架(6)- EF增删改查


离群索居 不是野兽便是神灵