部分更新的对象与EF code首先和ASP.NET MVC [英] Partially updating object with EF Code First and ASP.NET MVC

查看:106
本文介绍了部分更新的对象与EF code首先和ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EF4.1- code-第一大师!

EF4.1-Code-First-Gurus!

我不知道是否有处理以下ASP.NET MVC 3 EF 4.1 code第一种情形更优雅的方式:可以说我们有以下波苏斯:

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.

在我的仓库,我有以下Update方法。方法节选字段的列表,这应该被更新(离开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领域的隐藏字段的看法,但我不想BLOAD的形式很多(有更多的字段)。

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?

我强烈AP preciate您的帮助!
里斯

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首先和ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆