Closed. This question is opinion-based 。它目前不接受答案。
想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文来回答。
4年前关闭。
Improve this question
我不确定如何设置我的数据模型。
我正在使用: MVC 5、EF 6.1.3
我有一个 Model 类,它有几个属性(用几个“必需”数据注释属性装饰以反射(reflect)创建的数据库表),这些字段使用我的 Controller 中的 viewModel 填充。
模型类:
CreateRequestViewModel 类:
但是,如果我想更新反射(reflect) Model 类的数据库表上的某些字段,我必须加载所有必需的属性,然后由于“必需”数据注释而再次保存它们。问题是我只需要更新一些但不是所有属性(例如:我不会更改 FirstName 或 LastName 值)。
我的问题 :我应该从我的 Model 类中删除“必需”数据注释属性,并在 viewModel 上为用户输入设置这些数据注释吗?如果我这样做,那么我将丢失数据库表上字段的“NOT NULL”约束,但它是通过我的模型 View 强制执行的。或者我应该加载整个模型对象并在我的 dbcontext 上调用 .SaveChanges() 之前再次保存所有属性?
注意 :我删除了模型和 viewModel 中的几个额外属性以缩短此处的代码。
编辑 ;
我最终实现了以下代码以避免更改模型。它似乎工作正常。
如果名字(和其他在模型中标记为必需的属性)是一个必填字段,那么你应该装饰你的模型(这样在表中,该字段将被标记为非空)以及 ViewModel(用于 ModelState 验证和如果您使用的是 jQuery 验证器,则在 ui 中显示错误消息)具有必需的属性 为了在更新时只更新少数属性,您不必加载整个实体,而是可以将特定属性的状态标记为已修改。
var entry = context.Entry(你的实体);
entry.Property(e=> e.YourChangedProperty1).IsModified = true;
entry.Property(e=> e.YourChangedProperty2).IsModified = true;
context.SaveChanges();
想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文来回答。
4年前关闭。
Improve this question
我不确定如何设置我的数据模型。
我正在使用: MVC 5、EF 6.1.3
我有一个 Model 类,它有几个属性(用几个“必需”数据注释属性装饰以反射(reflect)创建的数据库表),这些字段使用我的 Controller 中的 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; }
}
但是,如果我想更新反射(reflect) Model 类的数据库表上的某些字段,我必须加载所有必需的属性,然后由于“必需”数据注释而再次保存它们。问题是我只需要更新一些但不是所有属性(例如:我不会更改 FirstName 或 LastName 值)。
我的问题 :我应该从我的 Model 类中删除“必需”数据注释属性,并在 viewModel 上为用户输入设置这些数据注释吗?如果我这样做,那么我将丢失数据库表上字段的“NOT NULL”约束,但它是通过我的模型 View 强制执行的。或者我应该加载整个模型对象并在我的 dbcontext 上调用 .SaveChanges() 之前再次保存所有属性?
注意 :我删除了模型和 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");
最佳答案
var entry = context.Entry(你的实体);
entry.Property(e=> e.YourChangedProperty1).IsModified = true;
entry.Property(e=> e.YourChangedProperty2).IsModified = true;
context.SaveChanges();