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 6842443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:06:04+00:00 2026-05-27T00:06:04+00:00

I have an ASP.NET MVC view for editing a model object. The edit page

  • 0

I have an ASP.NET MVC view for editing a model object. The edit page includes most of the properties of my object but not all of them — specifically it does not include CreatedOn and CreatedBy fields since those are set upon creation (in my service layer) and shouldn’t change in the future.

Unless I include these properties as hidden fields they will not be picked up during Binding and are unavailable when I save the modified object in my EF 4 DB Context. In actuality, upon save the original values would be overwritten by nulls (or some type-specific default).

I don’t want to drop these in as hidden fields because it is a waste of bytes and I don’t want those values exposed to potential manipulation.

Is there a “first class” way to handle this situation? Is it possible to specify a EF Model property is to be ignored unless explicitly set?

  • 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-27T00:06:04+00:00Added an answer on May 27, 2026 at 12:06 am

    Use either:

    public bool SaveRecording(Recording recording)
    {
        // Load only the DateTime property, not the full entity
        DateTime oldCreatedOn = db.Recordings
           .Where(r => r.Id == recording.Id)
           .Select(r => r.CreatedOn)
           .SingleOrDefault();
    
        recording.CreatedOn = oldCreatedOn;
    
        db.Entry(recording).State = EntityState.Modified;
        db.SaveChanges();
    
        return true;
    }
    

    (Edit: The query only loads the CreatedOn column from the database and is therefore cheaper and faster than loading the full entity. Because you only need the CreatedOn property using Find would be unnecessary overhead: You load all properties but need only one of them. In addition loading the full entity with Find and then detach it afterwards could be shortcut by using AsNoTracking: db.Recordings.AsNoTracking().SingleOrDefault(r => r.Id == recording.Id); This loads the entity without attaching it, so you don’t need to detach the entity. Using AsNoTracking makes loading the entity faster as well.)

    Edit 2

    If you want to load more than one property from the database you can project into an anonymous type:

    public bool SaveRecording(Recording recording)
    {
        // Load only the needed properties, not the full entity
        var originalData = db.Recordings
           .Where(r => r.Id == recording.Id)
           .Select(r => new
           {
               CreatedOn = r.CreatedOn,
               CreatedBy = r.CreatedBy
               // perhaps more fields...
           })
           .SingleOrDefault();
    
        recording.CreatedOn = originalData.CreatedOn;
        recording.CreatedBy = originalData.CreatedBy;
        // perhaps more...
    
        db.Entry(recording).State = EntityState.Modified;
        db.SaveChanges();
    
        return true;
    }
    

    (End of Edit 2)

    Or:

    public bool SaveRecording(Recording recording)
    {
        Recording oldVersion = db.Recordings.Find(recording.Id);
    
        recording.CreatedOn = oldVersion.CreatedOn;
    
        // flag only properties as modified which did really change
        db.Entry(oldVersion).CurrentValues.SetValues(recording);
    
        db.SaveChanges();
    
        return true;
    }
    

    (Edit: Using CurrentValues.SetValues flags only properties as Modified which indeed have been changed compared to the original state in the database. When you call SaveChanges EF will sent only the properties marked as modified in an UPDATE statement to the database. Whereas setting the state in Modified flags all properties as modified, no matter if they really changed or not. The UPDATE statement will be more expensive because it contains an update for all columns.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a strongly typed asp.net-mvc view and the Model has a . Links
I have a ASP.NET MVC 3 view with esentially two forms, but which reside
i have an asp.net mvc view where the top of the page is a
Background: I have an ASP.NET MVC view page with a MultiSelectList in the View
I have an ASP.NET MVC 3 view using Razor and Knockout.js that is not
I have an asp.net mvc register view. This is the first page. If someone
I have an ASP.NET MVC view which contains checkboxes for user-defined categories. <td><% foreach
I have an ASP.NET MVC view on which I've placed jQuery tabs. I'm loading
I have an ASP.NET MVC Partial View that contains a Html.TextBox that is configured
I have this sort of format asp.net MVC View -> Service Layer -> Repository.

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.