我正在使用 C# 和实体框架代码优先开发 WCF Rest 服务。
我有一个User
包含 10 列的表,但这是所需列的 JSON 表示:
{
"Name": "user_1",
"Active": true,
"Gender": 1,
"Email": "email_1@hotmail.com",
"Birthday": "02/07/1971",
"City": "city_1",
"Country": "country_1"
}
这就是我更新User
using的方式Entity Framework Code First
:
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."}
我是否必须发送所有必填字段才能仅更新一个?