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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:47:17+00:00 2026-05-25T18:47:17+00:00

I have this entity service in my domain model with two datetime type properties

  • 0

I have this entity service in my domain model with two datetime type properties entrydate and updatedon.

When user in edit view make any changes and submit form back I want entrydate property of the postedback/modified object to be marked as unchanged so entrydate can’t be overwritten when performing updates.

public class Service
{
    public int ServiceID
    {
        get;
        set;

    }
    [Required(ErrorMessage="Please enter Name")]
    public string Name
    {
        get;
        set;
    }

    [Required(ErrorMessage="Please enter the duration for the service")]
    public short Duration
    {
        get;
        set;
    }


    [DataType(DataType.Date)]
    public DateTime EntryDate
    {
        get;
        set;
    }

    [DataType(DataType.Date)]
    public DateTime UpdatedOn
    {
        get;
        set;
    }



    public decimal Cost
    {
        get; set;

    }
}

Repository method that is persisting changes into db is as follows:

 public void InsertOrUpdate(Service service)
    {
        if (service.ServiceID == default(int)) {
            // New entity
            context.Services.Add(service);
        } else {
            // Existing entity

            context.Entry(service).State = EntityState.Modified;
        }
    }
  • 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-25T18:47:17+00:00Added an answer on May 25, 2026 at 6:47 pm

    You can reload the original entity from the database:

    else {
        // Existing entity
        var serviceInDb = context.Services.Find(service.ServiceID);
        service.EntryDate = serviceInDb.EntryDate;
        context.Entry(serviceInDb).CurrentValues.SetValues(service);
    }
    

    When you call SaveChanges later an UPDATE statement only for properties which have really changed will be sent to the database (has also benefits for other unchanged properties).

    Or just reload the EntryDate:

    else {
        // Existing entity
        var entryDateInDb = context.Services
            .Select(s => s.EntryDate)
            .Single(s => s.ServiceID == service.ServiceID);
        service.EntryDate = entryDateInDb;
        context.Entry(service).State = EntityState.Modified;
    }
    

    Another working but ugly approach is this:

    context.Services.Attach(service); // thanks to user202448, see comments
    
    context.Entry(service).Property(s => s.Name).IsModified = true;
    context.Entry(service).Property(s => s.Duration).IsModified = true;
    context.Entry(service).Property(s => s.UpdatedOn).IsModified = true;
    context.Entry(service).Property(s => s.Cost).IsModified = true;
    

    So, don’t set the EntryDate property to modified but all the other properties one by one.

    The approach which comes into mind …

    context.Entry(service).State = EntityState.Modified;
    context.Entry(service).Property(s => s.EntryDate).IsModified = false;
    

    … unfortunately doesn’t work because setting back a property to not-modified which is already marked as Modified is not supported and will throw an exception.

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

Sidebar

Related Questions

I have an entity data model and a domain service (it may be a
I have entity model like this (using EclipseLink and JPA 2.0): @Entity class A
Let's say you have a domain entity User and you want to support the
Let's say I have this entity (for Hibernate): @Entity public class Person { @Id
suppose that I have this RDBM table ( Entity-attribute-value_model ): col1: entityID col2: attributeName
I have this DependencyProperty which holds an entity with a property that is a
I have a JPA object which has a many-to-many relationship like this: @Entity public
I have an entity which looks something like this: (I'm coding to the web
I have a variable declared like this in a class: Entity *array[BOARD_SIZE][BOARD_SIZE]; I need
I have a Person and an Organisation Entity: Person looks like this: public class

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.