1
我正在用C#和Entity Framework Code First開發一個WCF Rest服務。DbEntityValidationException:是否必須發送所有必填字段以僅更新一個?
我有一個表User
有10列,但是這是需要的列JSON表示:
{
"Name": "user_1",
"Active": true,
"Gender": 1,
"Email": "[email protected]",
"Birthday": "02/07/1971",
"City": "city_1",
"Country": "country_1"
}
這是我如何使用Entity Framework Code First
更新User
:
private User InsertOrUpdateUser(User user)
{
try
{
using (var context = new MyContext())
{
context.Entry(user).State = user.UserId == 0 ?
EntityState.Added :
EntityState.Modified;
context.SaveChanges();
}
}
catch (Exception ex)
{
ctx.StatusCode = System.Net.HttpStatusCode.InternalServerError;
ctx.SuppressEntityBody = true;
}
return user;
}
當我發送此用戶:
{
"Name": "user_1-mod",
"UserId": 1
}
我得到以下錯誤:
[System.Data.Entity.Validation.DbEntityValidationException] =
{"Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."}
我一定要送所有必填字段只有一個更新?