c# – 我应该在Model或viewModel中放置“必需的”数据注释吗?

我不确定如何设置我的数据模型.

我正在使用:MVC 5,EF 6.1.3

我有一个Model类,它有几个属性(用几个“Required”数据注释属性装饰以反映创建的数据库表),这些字段是使用我的控制器中的viewModel填充的.

[HttpPost]
public ActionResult Create(CreateRequestViewModel viewModel)
{
    if (!ModelState.IsValid)
    {
        viewModel.Affiliations = _context.Affiliations.ToList();
        viewModel.Issues = _context.Issues.ToList();
        return View(viewModel);
    }

    var request = new Request
    {
        RequestDate = DateTime.Today,
        Status = "Open",
        FirstName = viewModel.FirstName,
        LastName = viewModel.LastName,
        AffiliationId = viewModel.Affiliation,
        IssueId = viewModel.Issue,
        LastModificationDate = DateTime.Now,
        RequestTypeId = 2,
    };

    _context.Requests.Add(request);
    _context.SaveChanges();

    return View("Success");
}

型号类:

public class Request
{
    [Display(Name = "Request ID")]
    public int Id { get; set; }

    [Required]
    [Display(Name = "Request Date")]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime RequestDate { get; set; }

    [Required]
    public string Status { get; set; }

    [Required]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [Required]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }

    [Display(Name = "Last Modified")]
    public DateTime LastModificationDate { get; set; }

    //navigations properties
    public Affiliation Affiliation { get; set; }
    public Issue Issue { get; set; }
    public RequestType RequestType { get; set; }

    //foreign keys
    [Display(Name = "Affiliation")]
    public byte AffiliationId { get; set; }
    [Display(Name = "Issue")]
    public int IssueId { get; set; }
    [Display(Name = "Request Type")]
    public byte RequestTypeId { get; set; }


}

CreateRequestViewModel类:

public class CreateRequestViewModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public string FullName { get; set; }


    public IEnumerable<Affiliation> Affiliations { get; set; }
    public IEnumerable<Issue> Issues { get; set; }

    [Required(ErrorMessage = "Required!")]
    public byte Affiliation { get; set; }
    [Required(ErrorMessage = "Required!")]
    public int Issue { get; set; }
}

但是,如果我想更新反映Model类的数据库表上的某些字段,我必须加载所有必需的属性,然后再次保存它们,因为“必需的”数据注释.问题是我只需要更新一些但不是所有属性(例如:我不会更改FirstName或LastName值).

我的问题:我应该从我的Model类中删除“Required”数据注释属性,并在viewModel上为用户输入设置这些数据注释吗?如果我这样做,那么我的数据库表中的字段会丢失“NOT NULL”约束,但它会通过我的modelView强制执行.或者我应该在我的dbcontext上调用.SaveChanges()之前加载整个Model对象并再次保存所有属性?

注意:我在模型和viewModel中删除了几个额外的属性以缩短代码.

编辑;
我最终实现了以下代码以避免更改模型.它似乎工作正常.

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Details(DetailsViewModel request)
    {

        var model = new Request
        {
            Id = request.Id,
            RequestDate = request.RequestDate,
            Status = request.Status,
            FirstName = request.FirstName,
            LastName = request.LastName,
            LastModificationDate = DateTime.Now,
            AffiliationId = request.AffiliationId,
            IssueId = request.IssueId,
            RequestTypeId = request.RequestTypeId,

        };

        _context.Requests.Attach(model);
        var entry = _context.Entry(model);
        entry.Property(e => e.Status).IsModified = true;
        entry.Property(e => e.AffiliationId).IsModified = true;
        entry.Property(e => e.IssueId).IsModified = true;
        entry.Property(e => e.RequestTypeId).IsModified = true;
        entry.Property(e => e.LastModificationDate).IsModified = true;

        _context.SaveChanges();

        return RedirectToAction("Requests");

最佳答案 >如果First Name(以及Model中标记为需要的其他属性)是必填字段,那么您应该修饰您的Model(以便在表中,该字段将被标记为非null)以及ViewModel(对于ModelState验证和在ui中显示错误消息,以防您使用jQuery验证器)和必需属性

>要在更新时仅更新少数属性,您不必加载整个实体,而是可以将特定属性的状态标记为已修改.

var entry = context.Entry(你的实体);
 entry.Property(e => e.YourChangedProperty1).IsModified = true;
 entry.Property(e => e.YourChangedProperty2).IsModified = true;
 context.SaveChanges();

点赞