使用EF Code First和ASP.NET MVC部分更新对象 [英] Partially updating object with EF Code First and ASP.NET MVC
问题描述
EF4.1-Code-First-Gurus!
EF4.1-Code-First-Gurus!
我想知道是否有更优雅的方式来处理以下ASP.NET MVC 3 EF 4.1代码第一场景:假设我们有以下POCO:
I wonder if there is a more elegant way to handle the following ASP.NET MVC 3 EF 4.1 Code First scenario: Lets say we have the following POCOs:
public class Entity
{
[Key]
public int Id { get; set; }
[ScaffoldColumn(false)]
public DateTime CreatedOn { get; set; }
[ScaffoldColumn(false)]
public DateTime ModifiedOn { get; set; }
}
和
public class Person : Entity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime Birthday { get; set; }
}
假设我们创建了一些标准的编辑视图,不包括CreatedOn / ModifiedOn字段,因为它们将被设置在存储库中而不是由用户设置。
Lets assume we have created some standard editing views, which are not including CreatedOn/ModifiedOn fields, because, they will be set in the repository and not by the user.
在我的存储库中,我有以下更新方法。除了应该更新的字段列表之外的方法(将CreatedOn / ModifiedOn字段退出):
In my repository I have the following Update method. The methods excepts a list of fields, which should be updated (leaving CreatedOn/ModifiedOn fields out):
public void Update(Person person, List<string> properties)
{
Person tmpPerson = context.People.Single(x => x.Id == person.Id);
context.People.Attach(tmpPerson);
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(person))
{
if (properties.Contains(descriptor.Name))
descriptor.SetValue(tmpPerson, descriptor.GetValue(person));
}
tmpPerson.ModifiedOn = DateTime.Now;
}
现在控制器正在调用此方法:
Now the controller is calling this method like this:
[HttpPost]
public ActionResult Edit(Person person)
{
if (ModelState.IsValid) {
personRepository.Update(person, new List<string> { "FirstName", "LastName", "Birthday"});
personRepository.Save();
return RedirectToAction("Index");
} else {
return View();
}
}
这一切都像一个魅力。但是,我真的不喜欢,我必须手动指定字段。你如何处理这个要求?当然,我可以将CreatedOn / ModifiedOn字段作为隐藏字段添加到视图中,但是我不想将表单加载到很多(还有更多字段)。
This all works like a charm. However, I really dislike, that I have to specify the fields manually. How would you handle this requirement? Of course I could add CreatedOn/ModifiedOn fields as hidden fields to the view, but I dont want to bload the form to much (There are much more fields).
也许这是一个类似的问题:
如何更新EF 4实体在ASP.NET MVC 3?
Maybe this is a similar question: How To Update EF 4 Entity In ASP.NET MVC 3?
我非常感谢您的帮助!
Joris
I highly appreciate your help! Joris
推荐答案
是的,有更优雅的版本:
Yes there is more elegant version:
public void Update(Person person, params Expression<Func<Person,object>>[] properties)
{
context.People.Attach(person);
DbEntityEntry<Person> entry = context.Entry(person);
foreach (var property in properties)
{
entry.Property(property).IsModified = true;
}
person.ModifiedOn = DateTime.Now;
}
您将以这种方式调用方法:
You will call the method this way:
[HttpPost]
public ActionResult Edit(Person person)
{
if (ModelState.IsValid)
{
personRepository.Update(person, p => p.FirstName,
p => p.LastName, p => p.Birthday);
personRepository.Save();
return RedirectToAction("Index");
}
else
{
return View(person);
}
}
这篇关于使用EF Code First和ASP.NET MVC部分更新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!