验证同时使用实体框架,以SQL Server数据库保存更改失败的一个或多个实体 [英] Validation failed for one or more entities while saving changes to SQL Server Database using Entity Framework
问题描述
我想我的编辑保存到数据库和我在ASP.NET MVC 3 / C#中使用实体框架code-第一,但我收到错误信息。在我的事件类中,我有日期时间和时间跨度的数据类型,但在我的数据库,我已经有分别的日期和时间。难道这是什么原因呢?我怎么能投在code相应的数据类型更改保存到数据库之前。
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请求的执行过程中发生未处理的异常。请查看有关错误的详细信息的堆栈跟踪以及它起源于code。
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:\\九月\\ MvcEventCalendar \\ MvcEventCalendar \\ \\控制器线路EventManagerController.cs:77
Source File: C:\sep\MvcEventCalendar\MvcEventCalendar\Controllers\EventManagerController.cs Line: 77
堆栈跟踪:
[DbEntityValidationException:验证失败的一个或多个实体。见'EntityValidationErrors了解详情财产。]
[DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.]
推荐答案
您可以提取所有信息的 DbEntityValidationException
通过以下code(你需要添加命名空间: System.Data.Entity.Validation
和 System.Diagnostics程序
你的使用到
列表):
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屋!