龙空技术网

NET+EF+MVC开发接口实现事务提交并批量插入功能

娱乐一分哥 291

前言:

如今同学们对“netmvcef教程”可能比较关心,我们都想要剖析一些“netmvcef教程”的相关资讯。那么小编在网络上搜集了一些关于“netmvcef教程””的相关知识,希望我们能喜欢,大家快快来了解一下吧!

NET+EF+MVC开发接口时会经常遇到实现事务提交并批量插入功能,需要引用第三方DLL文件(Z.EntityFramework.Extensions.dll)笔者以下给个实例,以供参考:

[HttpPost]

public HttpResponseMessage TH_ReleaseAssignment(object data)

{

HttpResponseMessage result = null;

var ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"数据有误\",\"data\":\"\"}";

string Sdata = data.ToString();

var admin = JsonConvert.DeserializeObject<dynamic>(Sdata);//动态对象

decimal TrialID = 0;

decimal KindergartenID = 0;

decimal Kinder_Class_ID = 0;

decimal Masterteacherid = 0;

string Masterteachername = null;

int Appendagetype = 0;

string attachment = null;

//作业内容

string TaskContext = null;

string title = null;

string startdate = null;

string enddate = null;

string coverphoto = null;

if (admin.TrialID != null)

if (admin.Masterteacherid != null)

{

Masterteacherid = admin.Masterteacherid;

}

if (admin.Masterteachername != null)

{

Masterteachername = admin.Masterteachername;

}

if (admin.Appendagetype != null)

{

Appendagetype = admin.Appendagetype;

}

if (admin.attachment != null)

{

attachment = admin.attachment;

}

if (admin.TaskContext != null)

{

TaskContext = admin.TaskContext;

}

if (admin.title != null)

{

title = admin.title;

}

if (admin.startdate != null)

{

startdate = admin.startdate;

}

if (admin.enddate != null)

{

enddate = admin.enddate;

}

if (admin.coverphoto != null)

{

coverphoto = admin.coverphoto;

}

else if (string.IsNullOrEmpty(startdate))

{

ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"开始时间不能为空\",\"data\":\"\"}";

result = new HttpResponseMessage { Content = new StringContent(ReturnStr, Encoding.GetEncoding("UTF-8"), "application/json") };

return result;

}

else if (string.IsNullOrEmpty(enddate))

{

ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"截止时间不能为空\",\"data\":\"\"}";

result = new HttpResponseMessage { Content = new StringContent(ReturnStr, Encoding.GetEncoding("UTF-8"), "application/json") };

return result;

}

else

{

TaskContext = System.Web.HttpUtility.UrlEncode(TaskContext);

string Token = admin.Token;

#region 验证token

if (!string.IsNullOrEmpty(Token))

{

int t = new WisdomStars.Bll.Api.Common().PUB_VerificationToken(Token);

if (t <= 0)

{

ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"登录失效\",\"data\":\"\"}";

result = new HttpResponseMessage { Content = new StringContent(ReturnStr, Encoding.GetEncoding("UTF-8"), "application/json") };

return result;

}

}

else

{

ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"token不能为空\",\"data\":\"\"}";

result = new HttpResponseMessage { Content = new StringContent(ReturnStr, Encoding.GetEncoding("UTF-8"), "application/json") };

return result;

}

#endregion 验证token

#region 发布

TransactionOptions transactionOption = new TransactionOptions();

//设置事务隔离级别

transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;

// 设置事务超时时间为60秒

transactionOption.Timeout = new TimeSpan(0, 1, 30);

using (TransactionScope trans = new TransactionScope(TransactionScopeOption.Required, transactionOption)) //需要事务回滚的地方外面套一层using (TransactionScope sc = new TransactionScope()){}

{

try

{

gpt_Kinder_Class classmodel = ef.gpt_Kinder_Class.Where(a => a.isdeleted == 0 && a.TrialID == TrialID && a.KindergartenID == KindergartenID && a.Kinder_Class_ID == Kinder_Class_ID).AsNoTracking().FirstOrDefault();

if (classmodel != null)

{

decimal AssignmentID = WisdomStarts.Common.Utils.GenerateIntID();

//DataTable dt = DbHelperSQL.GET_DataTable_List(sql);

//// 把DataTable转换为IList<UserInfo>

//IList<gpt_Deliveries> deliverieslist = ModelConvertHelper<gpt_Deliveries>.ConvertToModel(dt);

//学生列表

List<gpt_Kinder_NewEnrolment> babylist = ef.gpt_Kinder_NewEnrolment.Where(a => a.Cadetstatus == 1 && a.TrialID == TrialID && a.KindergartenID == KindergartenID && a.Kinder_Class_ID == Kinder_Class_ID).AsNoTracking().ToList();

if (babylist != null && babylist.Count > 0)

{

List<gpt_AssignmentEnclosure> Enclosurelist = new List<gpt_AssignmentEnclosure>();

string[] array;

if (Appendagetype > 0 && !string.IsNullOrEmpty(attachment))

{

array = attachment.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

if (array.Length > 0)

{

for (int i = 0; i < array.Length; i++)

{

if (!string.IsNullOrEmpty(array[i]))

{

//附件ID

decimal AssiEnclosureID = WisdomStarts.Common.Utils.GenerateIntID();

gpt_AssignmentEnclosure info = new gpt_AssignmentEnclosure();

info.AssiEnclosureID = AssiEnclosureID;

info.AssignmentID = AssignmentID;

info.filepath = array[i];

info.Appendagetype = (int)Appendagetype;

info.createtime = DateTime.Now;

Enclosurelist.Add(info);

if (Appendagetype == 1 && string.IsNullOrEmpty(coverphoto))

{

coverphoto = array[i];

}

}

}

}

}

gpt_Assignment model = new gpt_Assignment();

model.AssignmentID = AssignmentID;

model.TaskContext = TaskContext;

model.AssignmentTime = DateTime.Now;

model.Masterteacherid = Masterteacherid;

model.Masterteachername = Masterteachername + "老师";

model.Appendagetype = Appendagetype;

model.IsDel = 0;

//应交人数

model.AnswerNum = babylist.Count;

//实交人数

model.ActualNum = 0;

if (string.IsNullOrEmpty(attachment))

{

model.Appendagetype = 0;

}

model.title = title;

model.startdate = DateTime.Parse(startdate);

model.enddate = DateTime.Parse(enddate);

model.Kinder_Class_Name = classmodel.Kinder_Class_Name;

if (string.IsNullOrEmpty(coverphoto))

{

model.coverphoto = "/coverphoto/assignment.png";

}

else

{

model.coverphoto = coverphoto;

}

ef.gpt_Assignment.Add(model);

int t = ef.SaveChanges();

if (t > 0)

{

#region 生成交付作业明细

List<gpt_Deliveries> deliverieslist = new List<gpt_Deliveries>();

foreach (var item in babylist)

{

decimal DeliveriesID = WisdomStarts.Common.Utils.GenerateIntID();

gpt_Deliveries Deliveries = new gpt_Deliveries();

Deliveries.DeliveriesID = DeliveriesID;

Deliveries.AssignmentID = AssignmentID;

Deliveries.DeliveriesTime = DateTime.Now;

Deliveries.BabyName = item.BabyName;

Deliveries.TaskContext = "";

Deliveries.Appendagetype = 0;

Deliveries.Kinder_Class_ID = model.Kinder_Class_ID;

Deliveries.KindergartenID = model.KindergartenID;

Deliveries.TrialID = model.TrialID;

Deliveries.praise = 0;

Deliveries.comments = 0;

deliverieslist.Add(Deliveries);

}

//批量插入方法

ef.BulkInsert(deliverieslist);

ef.BulkSaveChanges();

#endregion 生成交付作业明细

#region 生成附件明细

if (Enclosurelist.Count > 0)

{//批量插入方法

ef.BulkInsert(Enclosurelist);

ef.BulkSaveChanges();

}

#endregion 生成附件明细

trans.Complete();

ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.成功 + "\",\"msg\":\"发布成功\",\"data\":\"\"}";

}

else

{

ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"发布失败\",\"data\":\"\"}";

}

}

}

}

catch (Exception e)

{

ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"发布失败\",\"data\":\"\"}";

WisdomStarts.Common.Log.NetLog.WriteTextLog("教师发布作业错误", e.ToString());

trans.Dispose();

}

}

#endregion 发布

}

result = new HttpResponseMessage { Content = new StringContent(ReturnStr, Encoding.GetEncoding("UTF-8"), "application/json") };

return result;

}

标签: #netmvcef教程