none
How can DbContext be refreshed without using new? RRS feed

  • Question

  • Is there a way to refresh a DbContext without recreating it using new?

    Here is the scenario that I handle frequently prompting me to ask this question:

    Some changes have been made to a DbContext instance, SaveChanges() fails due to some validation errors that are recorded and reported by try-catch.  I just want to program to move on and continue using the same instance of DbContext for other tasks.  Without refreshing it, the invalidate entities would stay and generate an exception whenever SaveChanges() is called.

    Any tip will be greatly appreciated.


    Hong

    Monday, December 31, 2012 4:46 PM

Answers

  • Hi Hong,

    I think you can do this with DbEntityValidationException. Please check this simple example:

                using (var context = new tstContext())
                {
                    Person p = new Person { Name = "Shanks" };
                    Person p2 = new Person { Name = "Tom" };
                    context.People.Add(p);
                    context.People.Add(p2);
                    try
                    {
                        context.SaveChanges();
                    }
                    catch (DbEntityValidationException deve)
                    {
                        foreach (var entityValidationError in deve.EntityValidationErrors)
                        {
                            var entry = entityValidationError.Entry;
                            var state = entry.State;//Get original state information.
                            entry.State = System.Data.EntityState.Detached;//Mark as detached to let context do not save this entity.
    
                            foreach (var validationError in entityValidationError.ValidationErrors)
                            {
                                Console.WriteLine(validationError.PropertyName);//Get property name.
                                Console.WriteLine(validationError.ErrorMessage);//Get error message.
                            }
                            context.SaveChanges();//Save the other entities.
                            entry.State = state;//Back to original state.
                        }
                        //throw;
                    }
                }

    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.



    Friday, January 4, 2013 6:35 AM

All replies

  • Hi Hong,

    Before you call saveChange, you need to validate the data first.

    Have a nice day.


    Ghost,
    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.

    Wednesday, January 2, 2013 1:19 AM
  • Hi Hong,

    I am not sure whether you mean there is some concurrency issues and you want to refresh the entities or not. If so , you can take a look at this page: http://msdn.microsoft.com/en-us/data/jj592904

    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 2, 2013 7:40 AM
  • No, it is not a concurrency issue, but a validation issue.  

    Thank you for the link.  I wish DbEntityValidationException had similar methods.

     

    Hong

    Wednesday, January 2, 2013 11:28 AM
  • Hi Hong,

    I think you can do this with DbEntityValidationException. Please check this simple example:

                using (var context = new tstContext())
                {
                    Person p = new Person { Name = "Shanks" };
                    Person p2 = new Person { Name = "Tom" };
                    context.People.Add(p);
                    context.People.Add(p2);
                    try
                    {
                        context.SaveChanges();
                    }
                    catch (DbEntityValidationException deve)
                    {
                        foreach (var entityValidationError in deve.EntityValidationErrors)
                        {
                            var entry = entityValidationError.Entry;
                            var state = entry.State;//Get original state information.
                            entry.State = System.Data.EntityState.Detached;//Mark as detached to let context do not save this entity.
    
                            foreach (var validationError in entityValidationError.ValidationErrors)
                            {
                                Console.WriteLine(validationError.PropertyName);//Get property name.
                                Console.WriteLine(validationError.ErrorMessage);//Get error message.
                            }
                            context.SaveChanges();//Save the other entities.
                            entry.State = state;//Back to original state.
                        }
                        //throw;
                    }
                }

    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.



    Friday, January 4, 2013 6:35 AM
  • Thanks a lot, Alexander.  This will do it.

    I was thinking in the same line, but I was looking at the wrong object - ValidationErrors instead of EntityValidationErrors, so could not find the Entry property.


    Hong

    Friday, January 4, 2013 10:35 AM