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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:35:05+00:00 2026-06-08T11:35:05+00:00

Im writing an web application with MVC using Entity Framework for my backend logic.

  • 0

Im writing an web application with MVC using Entity Framework for my backend logic. My problem is that I have an entity that has certain fields that should never be changed on an update. I am not really sure what the best way to solve this problem would be. There is going to be a lot of data processed in my application, so I cant afford to just hack up a solution.

Is it possible to just define the fields as readonly in the POCO entities ? Or should I write and entity framework extension class that validates all updates. Could it be done in the mapping files between EF and the actual database?

I am relatively new with EF, so I hope some of you might be able to give me some pointers!

Thanks!

  • 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-06-08T11:35:06+00:00Added an answer on June 8, 2026 at 11:35 am

    Well I would advice against ever using the EF classes in the View. You’re best bet is to construct ViewModel classes and use Automapper to map them from the EF classes.

    When you are updating records in the database though, you can control which fields in the ViewModel are used to update the existing fields in the EF class.

    The normal process would be:

    • Use the Id to get the latest version of the existing object out of the database.

    • If you are using optimistic concurrency control then check that the object has not been updated since the ViewModel was created (so check timestamp for example).

    • Update this object with the required fields from your ViewModel object.

    • Persist the updated object back to the database.

    Update to include Automapper examples:

    Let’s say your POCO is

    public class MyObject 
    {
       public int Id {get;set;}
       public string Field1 {get;set;}
       public string Field2 {get;set;}
    }
    

    and Field1 is the field you don’t want updating.

    You should declare a view model with the same properties:

    public class MyObjectModel 
    {
       public int Id {get;set;}
       public string Field1 {get;set;}
       public string Field2 {get;set;}
    }
    

    and Automap between them in the constructor of your Controller.

    Mapper.CreateMap<MyObject, MyObjectModel>();
    

    you can if you wish (although I prefer to do this manually, automap the other way too:

    Mapper.CreateMap<MyObjectModel, MyObject>().ForMember(dest=>dest.Field1, opt=>opt.Ignore());
    

    When you are sending date to your website you would use:

     var myObjectModelInstance = Mapper.Map<MyObject, MyObjectModel>(myObjectInstance);
    

    to create the viewModel.

    When saving the data, you’d probably want something like:

    public JsonResult SaveMyObject(MyObjectModel myModel)
    {
        var poco = Mapper.Map<MyObjectModel, MyObject>(myModel);
        if(myModel.Id == 0 ) 
        {
           //New object
           poco.Field1 = myModel.Field1 //set Field1 for new creates only
    
        }
    }
    

    although I’d probably remove the exclusion of Field1 above and do something like:

    public JsonResult SaveMyObject(MyObjectModel myModel)
    {
       var poco;
       if(myModel.Id == 0)
       {
         poco = Mapper.Map<MyObjectModel, MyObject>(myModel);
       }        
       else
       {
         poco = myDataLayer.GetMyObjectById(myModel.Id);
         poco.Field2 = myModel.Field2;
       }
       myDataLayer.SaveMyObject(poco);
    }
    

    note I believe that best-practise would have you never Automap FROM the ViewModel, but to always do this manually, including for new items.

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

Sidebar

Related Questions

I am writing an ASP.Net MVC 3 Web Application using Entity Framework 4.1. My
I am writing a web application on Asp.Net MVC 3. Assume that we have
I am using codeigniter php MVC framework to develop a web application. I have
I'm developing a web application using ASP.NET MVC 3 and Entity Framework Code First
I'm writing a web application that connects to a database. I'm currently using a
I am writing web application using ASP.NET MVC + NHibernate + Postres stack. I
I'm writing web application that uses Spring MVC to bind Spring beans with REST-like
I am writing a Spring Restful webservices application using Spring MVC. I have used
i am using mvc struts framework in my web application. i am displaying dynamic
Im writing an web application using the MVC design pattern.. the application should connect

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.