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

来源:互联网 发布:php好吗 编辑:程序博客网 时间:2024/05/22 03:11

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

基本思路是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 0
原创粉丝点击