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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:56:07+00:00 2026-05-24T01:56:07+00:00

I have a status field on a class that has an ID and a

  • 0

I have a status field on a class that has an ID and a Name. I’m not using an enum to model it, but rather a class with some static values, like this:

    public class MailoutStatus : IEntity
{
    public static MailoutStatus Draft = new MailoutStatus() { Id = 1, Name = "Draft" };
    public static MailoutStatus Scheduled = new MailoutStatus() { Id = 2, Name = "Scheduled" };
    public static MailoutStatus Cancelled = new MailoutStatus() { Id = 3, Name = "Cancelled" };
    public static MailoutStatus Sent = new MailoutStatus() { Id = 4, Name = "Sent" };

    public int Id { get; set; }
    public string Name { get; set; }
...
}

Now I want to set this status value on the object it describes, like so:

        var repo = new MailoutRepository();
        var mailout = repo.Get(1);
        mailout.Status = MailoutStatus.Cancelled;
        repo.Update(mailout);
        repo.CommitChanges();

However, this code will see MailoutStatus.Cancelled as a new entity and will insert a new row into the MailoutStatus table, ignoring the ID that is already on Cancelled and adding a new IDENTITY generated ID (for instance, 5). I can prevent this by adding an entityvalidation stuff, but that just makes the above blow up due to the validation failure.

I can work around the issue using this code:

    var repo = new MailoutRepository();
    var mailout = repo.Get(1);
    mailout.Status = new MailoutStatusRepository().Get(MailoutStatus.Cancelled.Id);
    repo.Update(mailout);
    repo.CommitChanges();

This works because now Entity Framework knows about the MailoutStatus that I’m fetching and is tracking its state, etc. But it’s really crappy to have to write that much code just to set a status. I also don’t want to use an enum for other reasons and I don’t want MailoutStatus to know anything about persistence. Any ideas?

  • 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-24T01:56:08+00:00Added an answer on May 24, 2026 at 1:56 am

    Here’s how I solved it.

    I defined an attribute named NotTrackedAttribute and apply that on entities like Status. Then override the SaveChanges method of the derived context as follows. Reset the tracked changes to those entities

        public override int SaveChanges()
        {
            var changedEntities = ChangeTracker.Entries();
    
            foreach (var changedEntity in changedEntities)
            {
                 var entity = changedEntity.Entity;
    
                 //ignore the types that are marked as NotTracked
                 if (Attribute.IsDefined(entity.GetType(), typeof(NotTrackedAttribute)))
                 {
                     changedEntity.State = EntityState.Unchanged;
                     continue;
                 }
            }
    
            return base.SaveChanges();
        }
    

    The attribute

    /// <summary>
    /// Indicates that a Type having this attribute should not be persisted.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
    public class NotTrackedAttribute : Attribute
    {
    }
    

    Then use it as follows

    [NotTracked]
    public class MailoutStatus
    {
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model which has a field status definde as: class Model(models.Model): ...
I have two models class Employer(models.Model): name = models.CharField(max_length=300, blank=False) id = models.IntegerField() status
I have a class which has a field the same name as a method
I have an UPDATE statement that's intended to update a status field for a
First some background: I already have a Result class that I've implemented to carry
My overall use case: I have a Listing model that has many images .
I Have an Enum Called ActionStatus that has 2 possible values open=0 and closed
I have a simple query which is returning records based on the field status
Hey, I have a field called STATUS and it is either 1 to show
In a database prototype, I have a set of fields (like name, description, status)

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.