locked
Canceling updates after error condition RRS feed

  • Question

  • I’m probably missing something because I have a very silly problem with entity framework.

    If an error occurs while I try to persist modification using DbContex.savechanges  (for instance duplicate key) I am unable to cancel modifications in order to regain control.

    I’m under the impression that EF tries to save data prior to any other instruction, falling again into the error condition. A “reload” operation that will work prior to the error condition will not be executable after.

    My question is how do I cancel this “save changes” state. If it makes sense.

    Thanks in advance for your help

    Bernard


    BB

    Tuesday, January 8, 2013 9:56 AM

Answers

  • Hi Bernard,

    I am not sure what specific exception you get, so I just show you a simple sample.

        public class Person
        {
            public Int32 Id { get; set; }
            public String Name { get; set; }
        }
    
        public class tstContext : DbContext
        {
            public DbSet<Person> People { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Person>().Property(p => p.Name).HasMaxLength(5);
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                using (var context = new tstContext())
                {
                    Person p = new Person { Name = "Alexander" };
                    context.People.Add(p);
                    var p2 = context.People.First();
                    p2.Name = "Bernard";
                    try
                    {
                        context.SaveChanges();
                    }
                    catch (Exception ex)//Length of name is more than 5, so exception will be thrown.
                    {
                        foreach (var ee in context.ChangeTracker.Entries())
                        {
                            if (ee.State == System.Data.EntityState.Added)
                            {
                                ee.State = System.Data.EntityState.Detached;
                            }
                            if (ee.State == System.Data.EntityState.Modified)
                            {
                                ee.GetDatabaseValues();//Revert to database values.
                                ee.State = System.Data.EntityState.Unchanged;
                            }
                        }
                    }
                }
            }
        }
    

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alexander Sun Wednesday, January 16, 2013 9:38 AM
    Friday, January 11, 2013 9:23 AM

All replies

  • Hi Bernard,

    Welcome to the MSDN forum.

    Do you mean you want to continue to run your program after an error occurs while you call SaveChange?

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 9, 2013 6:27 AM
  • Hello Alexander,

    Program is running “correctly” even after the error (except if I try to manipulate datacontext) because I’m trapping the error. What I want to do is to revert changes after the error have been caught. Something like:

            For Each ee In db.ChangeTracker.Entries

                If ee.State = Data.EntityState.Modified Then

                    ee.Reload()

                End If

            Next

    This is not executing (neither in nor after the catch section) it is giving me back the same error.

     

    I forgot to say that EF is 4.1, source code is in VB (visible from aboveJ), and application is written using CodeFirst strategy. Data is bound to a datagrid and some other text fields.

    Thanks for your interest,

    Bernard


    BB

    Wednesday, January 9, 2013 10:15 AM
  • Hi Bernard,

    I am not sure what specific exception you get, so I just show you a simple sample.

        public class Person
        {
            public Int32 Id { get; set; }
            public String Name { get; set; }
        }
    
        public class tstContext : DbContext
        {
            public DbSet<Person> People { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Person>().Property(p => p.Name).HasMaxLength(5);
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                using (var context = new tstContext())
                {
                    Person p = new Person { Name = "Alexander" };
                    context.People.Add(p);
                    var p2 = context.People.First();
                    p2.Name = "Bernard";
                    try
                    {
                        context.SaveChanges();
                    }
                    catch (Exception ex)//Length of name is more than 5, so exception will be thrown.
                    {
                        foreach (var ee in context.ChangeTracker.Entries())
                        {
                            if (ee.State == System.Data.EntityState.Added)
                            {
                                ee.State = System.Data.EntityState.Detached;
                            }
                            if (ee.State == System.Data.EntityState.Modified)
                            {
                                ee.GetDatabaseValues();//Revert to database values.
                                ee.State = System.Data.EntityState.Unchanged;
                            }
                        }
                    }
                }
            }
        }
    

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alexander Sun Wednesday, January 16, 2013 9:38 AM
    Friday, January 11, 2013 9:23 AM