16

我有一个仅封装了一些数据库模型属性的视图模型。视图模型包含的这些属性是我要更新的唯一属性。我希望其他属性保持其价值。

在我的研究中,我发现这个 答案似乎非常适合我的需求,但是,尽管我尽了最大努力,但我无法让代码按预期工作。

这是我想出的一个孤立的例子:

static void Main() {
    // Person with ID 1 already exists in database.

    // 1. Update the Age and Name.
    Person person = new Person();
    person.Id = 1;
    person.Age = 18;
    person.Name = "Alex";

    // 2. Do not update the NI. I want to preserve that value.
    // person.NINumber = "123456";

    Update(person);
}

static void Update(Person updatedPerson) {
    var context = new PersonContext();

    context.Persons.Attach(updatedPerson);
    var entry = context.Entry(updatedPerson);

    entry.Property(e => e.Name).IsModified = true;
    entry.Property(e => e.Age).IsModified = true;

    // Boom! Throws a validation exception saying that the 
    // NI field is required.
    context.SaveChanges();
}

public class PersonContext : DbContext {
    public DbSet<Person> Persons { get; set; }
}

public class Person {
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required] 
    public int Age { get; set; } // this is contrived so, yeah.
    [Required]
    public string NINumber { get; set; }
}

我究竟做错了什么?

4

3 回答 3

4

您的工作基于帖子https://stackoverflow.com/a/15339512/2015959,但在另一个线程中,未更改的字段(因此不在附加模型中)不是强制性的,这就是它起作用的原因。由于您的字段是必需的,因此您将收到此验证错误。

您的问题可以通过部分更新的实体框架验证中提供的解决方案来解决

于 2014-05-14T19:54:20.507 回答
3

这是导致它不被保存的验证。您可以禁用验证,context.Configuration.ValidateOnSaveEnabled = false;它会起作用。要验证特定字段,您可以调用var error = entry.Property(e => e.Name).GetValidationErrors();. 因此,您当然可以创建一个仅强制执行业务规则并将这些属性标记为已修改的“UpdateNameAndAge”方法。无需双重查询。

  private static bool UpdateNameAndAge(int id, string name, int age)
  {
     bool success = false;

     var context = new PersonContext();
     context.Configuration.ValidateOnSaveEnabled = false;

     var person = new Person() {Id = id, Name = name, Age = age};
     context.Persons.Attach(person);
     var entry = context.Entry(person);

     // validate the two fields
     var errorsName = entry.Property(e => e.Name).GetValidationErrors();
     var errorsAge = entry.Property(e => e.Age).GetValidationErrors();

     // save if validation was good
     if (!errorsName.Any() && !errorsAge.Any())
     {
        entry.Property(e => e.Name).IsModified = true;
        entry.Property(e => e.Age).IsModified = true;

        if (context.SaveChanges() > 0)
        {
           success = true;
        }
     }

     return success;
  }
于 2014-05-09T15:04:39.567 回答
0

(为清楚起见进行了编辑)

上下文必须具有对象的完整副本才能强制执行业务规则。仅当附加对象已填充所有必要属性或部分视图在更新之前与完整副本合并时,才会发生这种情况。

我相信您想要做的事情在概念上是不可能的:进行这样的更新将需要保留的更改前副本,或者需要对数据库进行两次查询,因为业务层需要对象的完整副本进行验证。

于 2014-05-09T14:28:14.040 回答