EF 通用数据层父类方法小结

MSSql 数据库 数据层 父类

增删改查:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;

namespace DAL
{

   /// <summary>
    /// MSSql 数据库 数据层 父类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseDAL<T> where T : class,new()
    {
        /// <summary>
        /// EF 上下文对象
        /// </summary>
        DbContext db = new DBContextFactory().GetDbContext();
#region 1.0 新增实体,返回受影响的行数 +  int Add(T model)
        /// <summary>
        /// 1.0 新增实体,返回受影响的行数
        /// </summary>
        /// <param name="model"></param>
        /// <returns>返回受影响的行数</returns>
        public int Add(T model)
        {
            db.Set<T>().Add(model);
            //保存成功后,会将自增的id设置给model的主键属性,并返回受影响的行数。
            return db.SaveChanges();
        }
        #endregion

        #region 1.1 新增实体,返回对应的实体对象 + T AddReturnModel(T model)
        /// <summary>
        /// 1.1 新增实体,返回对应的实体对象
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public T AddReturnModel(T model)
        {
            db.Set<T>().Add(model);
            db.SaveChanges();
            return model;
        }
        #endregion

相关文章:

  • 2021-10-24
  • 2022-01-23
  • 2021-10-19
  • 2021-11-12
  • 2022-12-23
  • 2021-12-30
  • 2021-07-24
  • 2021-04-13
猜你喜欢
  • 2022-12-23
  • 2021-09-09
  • 2021-08-07
  • 2022-12-23
  • 2022-12-23
  • 2023-03-28
相关资源
相似解决方案