c# mvc 中linq和ef配合实现批量插入数据

原来的批量插入数据的方式(单个实体)

基本思路是new实体然后拼好 在通过foreach调用事务dbContext.InsertNotSaveChanges();  foreach结束后保存事务

这样做linq生成的sql很混乱而且效率很低 代码示例如下:

/// <summary>
        /// 保存招生进度新增的办理试听
        /// </summary>
        /// <param name="customerIds">客户ids</param>
        /// <param name="classId">班级id</param>
        /// <param name="courseId">课程id</param>
        /// <param name="sectioinId">小节id</param>
        public void CreateCustomerAuditionSections(List<int> customerIds, int classId, int courseId, int sectioinId)
        {
            using (var dbContext = new TopOnlineDbContext())
            {
                foreach (var customerId in customerIds)
                {
                    //构建试听表实体
                    var newAudition = new T_Audition()
                    {
                        Appointment = DateTime.Now,
                        CustomerId = customerId,
                        ClassId = classId,
                        CourseId = courseId,
                        SectionId = sectioinId,
                        AuditionStatus = 0,
                        IsDel = false,
                        CreateTime = DateTime.Now
                    };
                    dbContext.InsertNotSaveChanges(newAudition);
                }
                dbContext.SaveChanges();
            }
        }
 改进代码:先拼成list 再用dbContext.BatchInsert(newAuditionList)将list插入db

/// <summary>
        /// 保存招生进度新增的办理试听
        /// </summary>
        /// <param name="customerIds">客户ids</param>
        /// <param name="classId">班级id</param>
        /// <param name="courseId">课程id</param>
        /// <param name="sectioinId">小节id</param>
        public void CreateCustomerAuditionSections(List<int> customerIds, int classId, int courseId, int sectioinId)
        {
            using (var dbContext = new TopOnlineDbContext())
            {
                var newAuditionList = new List<T_Audition>();
                foreach (var customerId in customerIds)
                {
                    //构建试听表实体
                    var newAudition = new T_Audition()
                    {
                        Appointment = DateTime.Now,
                        CustomerId = customerId,
                        ClassId = classId,
                        CourseId = courseId,
                        SectionId = sectioinId,
                        AuditionStatus = 0,
                        IsDel = false,
                        CreateTime = DateTime.Now
                    };
                    newAuditionList.Add(newAudition);
                }
                dbContext.BatchInsert(newAuditionList);
            }
        }

BatchInsert方法:

 /// <summary>
        /// 批量插入数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public int BatchInsert<T>(List<T> list) where T : ModelBase
        {
            this.Set<T>().AddRange(list);
            this.SaveChanges();
            return list.Count;
        }

进阶:

批量插入主表时同时批量插入从表的方法

直接代码实例 应该很好理解了 T_Customer客户表和T_FollowLog表是一对多的关系 要在ef的实体和DbContext配置好关联

List<T_Customer> tCustomers;
//维护客户表关联的跟进表
                    foreach (var tCustomer in tCustomers)
                    {
                        var tFollowLog = new T_FollowLog
                        {
                            FollowTime = DateTime.Now,
                            FollowStatus = tCustomer.FollowStatus,
                            Content = "",
                            NextFollowTime = Convert.ToDateTime(tCustomer.NextFollowTime),
                            NeedFollow = tCustomer.NeedFollow,
                            IsDel = false,
                            IsUsed = true,
                            CreateTime = DateTime.Now
                        };
                        var followLogList = new List<T_FollowLog>() { tFollowLog };
                        tCustomer.TFollowLogList = followLogList;
                    }
                    //插入客户表和跟进表
                    var insertResult = EnrollService.InsertCustoms(tCustomers);

EnrollService.InsertCustoms()方法:

/// <summary>
        /// 插入客户信息和跟进信息(excel中批量插入)
        /// </summary>
        /// <param name="tCustomers">客户信息实体</param>
        /// <returns>true表示成功,false表示失败</returns>
        public bool InsertCustoms(List<T_Customer> tCustomers)
        {
            using (var dbContext = new TopOnlineDbContext())
            {
                //批量插入拼好的客户和跟进记录实体
                var result = dbContext.BatchInsert(tCustomers);
                return result > 0;
            }
        }

结束撒花

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值