猿问

使用实体框架将更改保存到SQL Server数据库时,一个或多个实体的验证失败

我想将“编辑”保存到数据库,并且在ASP.NET MVC 3 / C#中使用“实体框架”代码优先,但出现错误。在我的Event类中,我有DateTime和TimeSpan数据类型,但是在我的数据库中,我分别有Date和time。这可能是原因吗?将更改保存到数据库之前,如何在代码中强制转换为适当的数据类型。


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()中的问题;


// 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


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  

“ /”应用程序中的服务器错误。


一个或多个实体的验证失败。有关更多详细信息,请参见'EntityValidationErrors'属性。


说明:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。


异常详细信息:System.Data.Entity.Validation.DbEntityValidationException:对一个或多个实体的验证失败。有关更多详细信息,请参见'EntityValidationErrors'属性。


函数式编程
浏览 674回答 3
3回答

慕仙森

您可以DbEntityValidationException使用以下代码从中提取所有信息(您需要将命名空间:System.Data.Entity.Validation和添加System.Diagnostics到using列表中):catch (DbEntityValidationException dbEx){&nbsp; &nbsp; foreach (var validationErrors in dbEx.EntityValidationErrors)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; foreach (var validationError in validationErrors.ValidationErrors)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Trace.TraceInformation("Property: {0} Error: {1}",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; validationError.PropertyName,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; validationError.ErrorMessage);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

catspeake

如果您的类具有相同的属性名称,这是Praveen答案的一个小扩展:&nbsp;catch (DbEntityValidationException dbEx)&nbsp;{&nbsp; &nbsp; foreach (var validationErrors in dbEx.EntityValidationErrors)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;foreach (var validationError in validationErrors.ValidationErrors)&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Trace.TraceInformation(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Class: {0}, Property: {1}, Error: {2}",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; validationErrors.Entry.Entity.GetType().FullName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; validationError.PropertyName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; validationError.ErrorMessage);&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }&nbsp;}
随时随地看视频慕课网APP
我要回答