C# ASP.NET MVC:空外键ID导致验证错误
短篇小说: 我从db加载一个模型实例,对其应用更改,但当我想更新更改时,在模型定义中设置为C# ASP.NET MVC:空外键ID导致验证错误,c#,asp.net,asp.net-mvc,entity-framework,asp.net-mvc-5,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;
[必需]
的外键项会发生验证异常。我已经找到了解决办法,但我不知道什么是解决这个问题的正确方法
详细信息:
我有一个模型如下:
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; }
}
public DbSet<Project> Projects { get; set; }
每当我从db中获取模型信息并对其应用更改时,我都会遇到异常,这些异常是必需的ProjectManager
和Client
Project 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
这是因为当我从DB、ClientID和ProjectManagerID中获取信息时为空,并且如果我按以下方式手动设置它们,它将修复:
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);
}
我怀疑virtual
设置,但不确定,因为加载了外来对象,但其ID为空
p.S.I在AppDbContext中宣布的项目如下:
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; }
}
public DbSet<Project> Projects { get; set; }
公共数据库集项目{get;set;}
问题是我错过了foreignkey的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; }
}
公共类项目
{
公共int Id{get;set;}
[必需]
[StringLength(100,最小长度=5]
公共字符串名称{get;set;}
[必需]
公共虚拟应用程序用户客户端{get;set;}
[外国钥匙(“客户”)]
公共字符串ClientID{get;set;}//