ASP.NET MVC:空外键ID导致验证错误

时间:2016-03-04 18:27:55

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

短篇小说:

我从db加载模型实例,对其应用更改但是当我想更新更改时,对于在模型定义中设置为public class Project { public int Id { get; set; } [Required] [StringLength(100, MinimumLength = 5] public string Name { get; set; } [Required] public virtual ApplicationUser Client { get; set; } [Key] [ForeignKey("Client")] public string ClientID; [Required] public virtual ApplicationUser ProjectManager { get; set; } [Key] [ForeignKey("ProjectManager")] public string ProjectManagerID; [Range(0,100)] [Required] public int Progress { get; set; } [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTime CreateDate { get; set; } [Column("Disabled")] public bool Disabled{ get; set; } [Column("Status")] public string Status{ get; set; } } 的外键项,会发生验证异常。我找到了解决办法,但我不知道解决这个问题的正确方法是什么?

详细信息:

我有一个模型如下:

ProjectManager

每当我将数据库中的模型信息提取到实例中时,应用对其进行更改,我将面临需要ClientProject currentProject = (from prj in dbContext.Projects where prj.Id == project.Id).Single(); //---> currentProject.ProjectManagerID is null //---> currentProject.ClientID is null //---> currentProject.ProjectManager is present //---> currentProject.Client is present currentProject.Name = .... currentProject.Progress = .... currentProject.Status = .... currentProject.Disabled = .... dbContext.Entry(currentProject).State = System.Data.Entity.EntityState.Modified; dbContext.SaveChanges(); //--> Validation error for required Client,ProjectManager 的异常。

            currentProject.Name = project.Name;
            currentProject.Progress = project.Progress;
            currentProject.Status = project.Status;
            currentProject.Disabled = project.Disabled;
            currentProject.ProjectManagerID = currentProject.ProjectManager.Id;
            currentProject.ClientID = currentProject.Client.Id;
            try
            {
                dbContext.Entry(currentProject).State = System.Data.Entity.EntityState.Modified;
                dbContext.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                addValidationErrorsToModelState(e);
                return View(project);
            }

这是因为当我从DB获取信息时,ClientID和ProjectManagerID为空,如果我手动设置它们如下,它将修复:

virtual

我对 public DbSet<Project> Projects { get; set; } 设置有疑问但不确定,因为加载了外来对象,但其ID为空。

P.S。我在AppDbContext中声明项目如下:

{{1}}

1 个答案:

答案 0 :(得分:0)

问题是我错过了ForeignKeys的ID属性的setter / getter:

public class Project
{
    public int Id { get; set; }

    [Required]
    [StringLength(100, MinimumLength = 5]
    public string Name { get; set; }

    [Required]
    public virtual ApplicationUser Client { get; set; }


    [ForeignKey("Client")]
    public string ClientID{ get; set; } //<--fixed 


    [Required]
    public virtual ApplicationUser ProjectManager { get; set; }


    [ForeignKey("ProjectManager")]
    public string ProjectManagerID { get; set; } //<--fixed 

    [Range(0,100)]
    [Required]
    public int Progress { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime CreateDate { get; set; }

    [Column("Disabled")]
    public bool Disabled{ get; set; }

    [Column("Status")]
    public string Status{ get; set; }

}