使用实体框架将更改保存到 SQL Server 数据库时,对一个或多个实体的验证失败 [英] Validation failed for one or more entities while saving changes to SQL Server Database using Entity Framework
问题描述
我想将我的编辑保存到数据库,我在 ASP.NET MVC 3/C# 中使用实体框架代码优先,但出现错误.在我的 Event 类中,我有 DateTime 和 TimeSpan 数据类型,但在我的数据库中,我分别有 Date 和 time.这可能是原因吗?在将更改保存到数据库之前,如何在代码中转换为适当的数据类型.
I want to save my Edit to Database and I am using Entity FrameWork Code-First in ASP.NET MVC 3 / C# but I am getting errors. In my Event class, I have DateTime and TimeSpan datatypes but in my database, I've got Date and time respectively. Could this be the reason? How can I cast to the appropriate datatype in the code before saving changes to database.
public class Event
{
public int EventId { get; set; }
public int CategoryId { get; set; }
public int PlaceId { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public DateTime EventDate { get; set; }
public TimeSpan StartTime { get; set; }
public TimeSpan EndTime { get; set; }
public string Description { get; set; }
public string EventPlaceUrl { get; set; }
public Category Category { get; set; }
public Place Place { get; set; }
}
控制器中的方法>>>> storeDB.SaveChanges() 中的问题;
Method in the controller >>>> Problem at storeDB.SaveChanges();
// POST: /EventManager/Edit/386
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
var theEvent = storeDB.Events.Find(id);
if (TryUpdateModel(theEvent))
{
storeDB.SaveChanges();
return RedirectToAction("Index");
}
else
{
ViewBag.Categories = storeDB.Categories.OrderBy(g => g.Name).ToList();
ViewBag.Places = storeDB.Places.OrderBy(a => a.Name).ToList();
return View(theEvent);
}
}
与
public class EventCalendarEntities : DbContext
{
public DbSet<Event> Events { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Place> Places { get; set; }
}
SQL Server 2008 R2 数据库/T-SQL
SQL Server 2008 R2 Database / T-SQL
EventDate (Datatype = date)
StartTime (Datatype = time)
EndTime (Datatype = time)
Http 表单
EventDate (Datatype = DateTime) e.g. 4/8/2011 12:00:00 AM
StartTime (Datatype = Timespan/time not sure) e.g. 08:30:00
EndTime (Datatype = Timespan/time not sure) e.g. 09:00:00
/"应用程序中的服务器错误.
Server Error in '/' Application.
一个或多个实体的验证失败.有关更多详细信息,请参阅EntityValidationErrors"属性.
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
描述:在执行当前 Web 请求的过程中发生了未处理的异常.请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
异常详细信息:System.Data.Entity.Validation.DbEntityValidationException:一个或多个实体的验证失败.有关更多详细信息,请参阅EntityValidationErrors"属性.
Exception Details: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
源错误:
Line 75: if (TryUpdateModel(theEvent))
Line 76: {
Line 77: storeDB.SaveChanges();
Line 78: return RedirectToAction("Index");
Line 79: }
源文件:C:sepMvcEventCalendarMvcEventCalendarControllersEventManagerController.cs 行:77
Source File: C:sepMvcEventCalendarMvcEventCalendarControllersEventManagerController.cs Line: 77
堆栈跟踪:
[DbEntityValidationException:一个或多个实体的验证失败.有关详细信息,请参阅EntityValidationErrors"属性.]
[DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.]
推荐答案
您可以使用以下代码从 DbEntityValidationException
中提取所有信息(您需要添加命名空间:System.Data.Entity.Validation
和 System.Diagnostics
到您的 using
列表):
You can extract all the information from the DbEntityValidationException
with the following code (you need to add the namespaces: System.Data.Entity.Validation
and System.Diagnostics
to your using
list):
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Trace.TraceInformation("Property: {0} Error: {1}",
validationError.PropertyName,
validationError.ErrorMessage);
}
}
}
这篇关于使用实体框架将更改保存到 SQL Server 数据库时,对一个或多个实体的验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!