Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4579152
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:36:06+00:00 2026-05-21T20:36:06+00:00

EF4.1-Code-First-Gurus! I wonder if there is a more elegant way to handle the following

  • 0

EF4.1-Code-First-Gurus!

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; }
}

and

public class Person : Entity
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Birthday { get; set; }
}

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.

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();
        }
    }

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).

Maybe this is a similar question:
How To Update EF 4 Entity In ASP.NET MVC 3?

I highly appreciate your help!
Joris

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T20:36:07+00:00Added an answer on May 21, 2026 at 8:36 pm

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a project in ASP.NET MVC using EF4 Code-First for the
I'm creating a multi-tenant Asp.Net MVC 3 Web app, and using EF4.1 code first
I've been reading Scott Gu's post on code first design with ASP.Net MVC 2.0
First some brief background: I have an existing ASP.NET MVC 1 application using Entity
using MVC3, Code First (DbContext) Is there a way to implement (ideally) a facade
Is there a way in Entity Framework 4 (using CTP4 and Code First if
I use asp.net 4 c# and ef4. I have this code, it should compile
I'm using Ninject with an MVC app, also using EF4.1 Code First. I'm getting
I am trying to teach my self MVC3 and EF4 using code first and
I'm getting to grips with EF4 code first, and liking it so far. But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.