尝试在WebApi控制器中使用EF6保存更改时出现异常。例外情况如下:
Failed to authenticate with one or more entities. For more information, see the property "EntityValidationErrors".
从堆栈跟踪中我发现了以下内容:
в System.Data.Entity.Internal.InternalContext.SaveChanges()
в System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
в System.Data.Entity.DbContext.SaveChanges()
в GuideOnline_1_.Controllers.xTouristController.PutxTourist(Int32 id, xTourist xtourist) в c:\Users\Administrator\Documents\Visual Studio 2013\Projects\GuideOnline(1)\GuideOnline(1)\Controllers\xTouristController.cs:строка 55
в lambda_method(Closure , Object , Object[] )
в System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
в System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
в System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
这是我的控制器(由VS自动生成)。是的,我曾经使用DB第一种方法来生成模型。
// PUT api/xTourist/5
public IHttpActionResult PutxTourist(int id, xTourist xtourist)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != xtourist.Kod)
{
return BadRequest();
}
db.Entry(xtourist).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!xTouristExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
我是WebApi和Entity Framework的新手,所以我希望得到有经验的同事的建议。提前致谢。 附: 我确信jQuery代码的一切都很好,它将值传递给xtourist对象和控制器以及sql部分,因为它在我运行纯sql脚本时工作正常。
P.P.S。我希望这是你正在寻找的,基里尔。
System.Data.Entity.Validation.DbEntityValidationException не обработано пользовательским кодом
HResult=-2146232032
Message=Сбой при проверке одной или нескольких сущностей. Более подробные сведения см. в свойстве "EntityValidationErrors".
Source=EntityFramework
StackTrace:
в System.Data.Entity.Internal.InternalContext.SaveChanges()
в System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
в System.Data.Entity.DbContext.SaveChanges()
в GuideOnline_1_.Controllers.xTouristController.PutxTourist(Int32 id, xTourist xtourist) в c:\Users\Administrator\Documents\Visual Studio 2013\Projects\GuideOnline(1)\GuideOnline(1)\Controllers\xTouristController.cs:строка 55
в lambda_method(Closure , Object , Object[] )
в System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
в System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
в System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
InnerException:
更新 最终的解决方案是:
try
{
var arrivals = db.xTourist.Find(id);
arrivals.Номер = item.Номер;
arrivals.Telefon = item.Telefon;
db.SaveChanges();
}