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);
}mvc和三层架构的理解
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;
}
}
结束撒花
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论