使用MVC4,Entity Framework和MySQL服务器我正在执行 entities.SaveChanges()来添加新实体(Record)。但执行大约需要15-20秒。
当我将其替换为:
Task.Factory.StartNew(() =>
{
entities.SaveChangesAsync();
});
执行的代码没有任何错误,但数据库中没有发生更改。
Controller中ActionResult的完整代码是:
MySQLContext entities = new MySQLContext();
ul = new userlesson();
courselesson cl = GlobalVariables.CourseLesson(intLessonID); //entities.courselessons.Where(m => m.LessonID == intLessonID).SingleOrDefault();
if (Request.IsAuthenticated || intLessonID==1)
{
ul.LessonID = intLessonID;
ul.UniqueAssesmentID =GlobalVariables.AssesmentID();
if (!String.IsNullOrEmpty(Email))
{
ul.Email = UserManager.User.Email;
}
FillCourseLessonToUserLesson(ref cl, ref ul, ul.UniqueAssesmentID);
entities.userlessons.Add(ul);
if (Request.IsAuthenticated)
{
entities.SaveChanges(); // Taking almost 20 sec
}
return View(ul);
}