添加注解后数据库生成会自动重新映射模型,导致代码消失,查找资料后需将数据库与实体类做一次映射解决:
//映射类型
[MetadataType(typeof(Model.ArticleVM))]//数据库类型
public partial class Contents_Article
{
}
//映射字段
public partial class ArticleVM
{
public int ID { get; set; }
[Required(ErrorMessage = "标题不能为空")]
public string Title { get; set; }
[Required(ErrorMessage = "内容不能为空")]
public string Centent { get; set; }
public int Status { get; set; }
public string Author { get; set; }
public Nullable<System.DateTime> AddDatetime { get; set; }
public Nullable<System.DateTime> UpdateDatetime { get; set; }
public string Summary { get; set; }
public string CreatedID { get; set; }
public int SortID { get; set; }
}
前台调用:
if (ModelState.IsValid)
{
if (new BLL.Article().Append(article))
{
return Content("添加成功");
}
}
return View();