EF6更新相对表值

时间:2016-07-11 13:42:36

标签: c# .net asp.net-mvc entity-framework

当用户点击页面上某个元素旁边的按钮时,我希望元素isAccepted值发生变化。按下按钮时,db.SaveChanges()命令会抛出EntityValidationErrors类型的异常。

控制器

namespace WebApplication6.Controllers
{
    public class AppController : Controller
    {        
         [HttpGet]
         public ActionResult Accept(int? id) 
         {

             EmailFormModel itemToUpdate = db.ProjectInfo.Find(id);

             itemToUpdate.isAccepted = true; //only value I want to change

             db.SaveChanges();


             return RedirectToAction("MainDbView");
        }
    }
}

模型

public class EmailFormModel
{
    public EmailFormModel()
    {
        isAccepted = false;
        Ressurs = new List<RessursBehov>() { new RessursBehov() };
    }

    public int id { get; set; }

    public bool isAccepted { get; set; }

    [Required(ErrorMessage = "påkrevet"), Display(Name = "Prosjektnummer"), RegularExpression(@"^([pP])\d*$", ErrorMessage = "Prosjektnummer er på formen: p1234 ")] 
    public string Prosjektnummer { get; set; }

    [Required(ErrorMessage = "påkrevet"), Display(Name = "Prosjektnavn")]
    public string Prosjektnavn { get; set; }

    [Required(ErrorMessage = "påkrevet"), Display(Name = "Prosjekttype")]
    public string Prosjekttype { get; set; }

    [Required(ErrorMessage = "påkrevet"), Display(Name = "Prosjektleder")]
    public string Prosjektleder { get; set; }

    [Required(ErrorMessage = "påkrevet"), Display(Name = "Skjema utfylt av")]
    public string Prosjektregistrerer { get; set; }

    [Required]
    public virtual List<RessursBehov> Ressurs { get; set; }

}
public class RessursBehov
{
    public int id { get; set; }    

    [Required(ErrorMessage = "påkrevet"), Display(Name = "Ressurstype")]
    public string Ressurstype { get; set; }

    [Required(ErrorMessage = "påkrevet"), Display(Name = "Navn på ressurs")]
    public string Navn_På_Ressurs { get; set; }

    [Required(ErrorMessage = "påkrevet"), Display(Name = "Ukenummer"), RegularExpression(@"^([1-9]|[1-4][0-9]|5[0-2])$", ErrorMessage = "Ugyldig verdi")]
    public int? Ukenummer { get; set; }

    [Required(ErrorMessage = "påkrevet"), Display(Name = "Antall timer"), RegularExpression(@"^([1-9]|[1-9][0-9])$", ErrorMessage = "Ugyldig verdi")]
    public int? Antall_Timer { get; set; }

    [Required(ErrorMessage = "påkrevet"), Display(Name = "Antall uker"), RegularExpression(@"^([1-9]|[1-9][0-9])$", ErrorMessage = "Ugyldig verdi")]
    public int? Antall_Uker { get; set; }

    [Required]
    public virtual EmailFormModel realemailformmodel { get; set; }
}

上下文类

public class TalkToDb : DbContext
{
   public DbSet<EmailFormModel> ProjectInfo { get; set; }

    public DbSet<RessursBehov> ProjectExtra { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //one-to-many 
        modelBuilder.Entity<RessursBehov>()
                    .HasRequired<EmailFormModel>(s => s.realemailformmodel)
                    .WithMany(s => s.Ressurs);
    }
}

更新

这是我得到的错误:

抛出异常:EntityFramework.dll中的'System.Data.Entity.Validation.DbEntityValidationException' iisexpress.exe信息:0:属性:Ressurstype错误:påkrevet

iisexpress.exe信息:0:属性:Navn_På_Ressurs错误:påkrevet

iisexpress.exe信息:0:属性:Ukenummer错误:påkrevet

iisexpress.exe信息:0:属性:Antall_Timer错误:påkrevet

iisexpress.exe信息:0:属性:Antall_Uker错误:påkrevet

1 个答案:

答案 0 :(得分:0)

该异常意味着您的实体验证失败。如果进行调试,则可以检查ModelState对象并查找具有非空Errors属性的项目。

或者,您可以将异常重写为更具描述性的内容:

try
{
    db.SaveChanges();
}
catch (DbEntityValidationException e)
{
    var errorMessages = e.EntityValidationErrors
        .SelectMany(x => x.ValidationErrors)
        .Select(x => x.ErrorMessage);

    var fullErrorMessage = string.Join("; ", errorMessages);
    var exceptionMessage = string.Concat(e.Message, " The validation errors are: ", fullErrorMessage);
    throw new DbEntityValidationException(exceptionMessage, e.EntityValidationErrors);
}

这实际上会将每个验证错误列为异常消息的一部分,这样可以更容易地告诉您需要修复的内容。如果您正在使用存储库或服务,您实际上可以将其构建到您的&#34; Save&#34;方法,因此它会自动发生,而不必在每次要保存实体时都使用try块。