【发布时间】:2019-05-27 01:57:10
【问题描述】:
LessonQuestionDetails中导航属性中的Required注解实体模型,支持通过代码优先方法创建级联删除表,导致ModelState.IsValid 变为false。没有Required 注释是否有解决方法来设置级联删除。
实体模型
public partial class LessonQuestionDetail
{
[Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.None)]
public int LessonID { get; set; }
[Key, Column(Order = 1), DatabaseGenerated(DatabaseGeneratedOption.None)]
public int QuestionNumber { get; set; }
[Key, Column(Order = 2), DatabaseGenerated(DatabaseGeneratedOption.None)]
public byte ChoiceNumber { get; set; }
public string Choice { get; set; }
public bool IsCorrect { get; set; }
[Required] // Sets the CASCADE constraint, while creating table
public virtual LessonQuestion LessonQuestion { get; set; }
}
public partial class LessonQuestion
{
public LessonQuestion()
{
this.LessonQuestionDetails = new List<LessonQuestionDetail>();
}
public virtual ICollection<LessonQuestionDetail> LessonQuestionDetails { get; set; }
//Other code
}
控制器
[HttpPost]
public ActionResult EditLessonQuestionDetails(LessonQuestion lq)
{
SQLContext context = new SQLContext();
int intChoiceNum=1;
var errors = ModelState.Values.SelectMany(v => v.Errors); // There are errors
var valid = ModelState.IsValid; // sets False
// Other code
}
【问题讨论】:
-
您不需要必填字段。您可以使用流畅的 API 来做到这一点。你会在谷歌上找到很多参考
-
什么有错误?
标签: c# asp.net-mvc entity-framework required modelstate