如何修复 entity framework 中的 'System.Data.Entity.Database.CurrentTransaction' 错误消息?

How to fix 'System.Data.Entity.Database.CurrentTransaction' error message in entity framework?

我在sqlserver2017中创建了一个数据库,并尝试使用entity framework6连接数据库。它已连接,编译时我没有任何错误,但我收到一些错误消息,如:Database.CurrentTransaction at runtime when want to create a record in the database.

/*inside the Controller*/

        [HttpGet]
        public ActionResult AddStudent()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddStudent(Student new_stud)
        {                        
            if(ModelState.IsValid)
            {
                BLStudent bls = new BLStudent();
                if(bls.Add(new_stud))
                {
                    ViewBag.message = "It's recorded.";
                    ViewBag.color = "aqua";
                }
                else
                {
                    ViewBag.message = "It's not recorded.";
                    ViewBag.color = "red";
                }
            }
            else
            {
                ViewBag.message = "PLS enter the information correctly.";
                ViewBag.color = "red";
            }

            return View();
        }
/*inside the BLStudent Class*/

        UniversityEntities db = new UniversityEntities();

        public bool Add(Student new_stud)
        {
            try
            {
                db.Students.Add(new_stud);
                return Convert.ToBoolean(db.SaveChanges());
            }
            catch (Exception)
            {

                return false;
            }
        }

Cath Exceptions :

System.Data.Entity.Infrastructure.DbUpdateException: 'An error occurred while updating the entries. See the inner exception for details.'

内部异常:

1 - UpdateException: An error occurred while updating the entries. See the inner exception for details.

2 - SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. The statement has been terminated.

如何解决这个问题?

根据完整的错误消息

" SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. The statement has been terminated."

您的数据库中似乎存在类型转换问题。

我建议将数据库中的 DateTime 列更改为 DateTime2。

显然,您在架构中使用 DateTime 类型,这是不推荐的,并且存在转换问题(可能是 C# 中的默认值,即 0001 年等,无法转换为 DateTime)