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

  • Home
  • SEARCH
  • 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 9029225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:12:00+00:00 2026-06-16T07:12:00+00:00

We are using Entity Framework Code First, and I am running into issues trying

  • 0

We are using Entity Framework Code First, and I am running into issues trying to rollback entity changes for Insert, Update, and Delete when we don’t want to SaveChanges().

Specifically, I have a datagridview which I am using as a TableEditor, to make changes to some auxiliary tables. The datagridview is bound to DbSet<TEntity>.

My rollback for Update seems to work fine, I just set the currentValues to back to their OriginalValues, and change state to unchanged.

When a record is inserted to the gridview (but no changes saved), it never shows up in the entity class, and I never see it again… So I guess it doesn’t make it to the dbSet, and no rollback is needed for this?

But my main problem lies with Delete:

From what I understand, when a record is “deleted” (eg.tableData.Remove(currentItem);), it is simply marked for deletion until SaveChanges is called. So if I change the State from deleted back to unchanged, that should handle the rollback, right?

Well, the record does show back up again, BUT the navigational properties of the record are gone! (ie. the columns containing foreign keys and required relationships to other entities). Why is this??!

Here is what I have so far:

    public void RollbackChanges(DbEntityEntry entry)
    {
        if (entry.State == EntityState.Modified)
        {
            foreach (var propertyName in entry.OriginalValues.PropertyNames)
            {
                entry.CurrentValues[propertyName] = entry.OriginalValues[propertyName];
            }
            entry.State = EntityState.Unchanged;
        }
        else if (entry.State == EntityState.Deleted)
        {
            entry.State = EntityState.Unchanged;
        }
        else if ((entry.State == EntityState.Added) || (entry.State == EntityState.Detached))
        {
            MessageBox.Show("I don't think this ever happens?");
        }
    }

Example usage:

    foreach (var entity in db.CertificationDecisions)
                {
                    DbEntityEntry entry = db.Entry(entity );
                    if (entry.State != EntityState.Unchanged)
                    {
                        RollbackChanges(entry);
                    }
                }

Any ideas why the navigational properties would disappear from the record? (Or what I can do to get them back?)


EDIT:
@Chris regarding using Refresh:

I am using a DbContext, so I replaced my rollback methods with this line:

((IObjectContextAdapter)db).ObjectContext.Refresh(RefreshMode.StoreWins, db.CertificationDecisions);

However, this does not seem to reload the context, as the record is still missing… Am I using Refresh wrong?

This sounds like a possible solution to my problem, but I am still wondering why the navigation properties would be removed?

  • 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-16T07:12:01+00:00Added an answer on June 16, 2026 at 7:12 am

    This is not really an answer to my question, but is the alternative that seems to be working for me so far (in case anyone is looking for an answer to this question):

    Basically, I create a new context and pass an entity from the new context to my form. If I cancel the form, I simply dispose of the context, so no changes are saved (no need to rollback). If I save the form, then I save the changes from the new context, but then I need to make my original context aware of the changes.

    So I use .Refresh, which seems to successfully find new entities, but does not remove deleted ones. So then I loop through the entities and compare them to the database values.

    If the database returns ‘null’, we know the entity has been deleted in the database and needs to be removed from the context. (However, I can’t remove it in the loop, as trying to change the contents of a context while enumerating through it will cause an error, so instead I add the entries to a list to remove from the context in a seperate loop afterwards.)

    Below is the code:
    (note: I give no guarantee that this is a good solution, but it seems to be working for me)

                using (FmlaManagementContext auxDb = new FmlaManagementContext())
                {
                    AuxiliaryTableEditor<State> statesTableEditor = new AuxiliaryTableEditor<State>(auxDb.States);
                    if (statesTableEditor.ShowDialog() != DialogResult.OK)
                    {
                        auxDb.Dispose();
                    }
                    else
                    {
                        auxDb.SaveChanges();
                        auxDb.Dispose();
    
                        //refresh to pick up any new
                        ((IObjectContextAdapter)db).ObjectContext.Refresh(RefreshMode.StoreWins, db.States);
    
                        //loop through to remove deleted
                        List<State> nulls = new List<State>();
                        foreach (State state in db.States.Local)
                        {
                            DbEntityEntry entry = db.Entry(state);
                            DbPropertyValues databaseValues = entry.GetDatabaseValues();
                            if (databaseValues == null)
                            {
                                nulls.Add(state); 
                            }
                        }
                        foreach (State state in nulls)
                        {
                            db.States.Remove(state);
                        }
                    }
                }
    

    If I end up changing this or finding problems with it, I will try to remember to come back and update…

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

Sidebar

Related Questions

I'm using the latest and greatest Entity Framework Code First and I'm running into
I am using Entity Framework Code First and ran into a small road block.
I am using Entity framework Code First CTP5 and I am trying to find
I'm trying to map an existing database using Entity Framework Code First Fluent API.
I'm trying to implement the repository pattern using entity framework code first rc 1.
I'm using Entity Framework Code First. The class i'm trying to create contains two
I'm creating a database using entity framework code first and I'm having some issues
When using Entity Framework Code First 4.3.1 it is possible to create relationships with
We are using Entity Framework Code First with Foreign Key relationships. We investigating on
I'm using Entity Framework Code First v 4.3.0 I have a two entities with

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.