Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
ValidationErrors is not reset?
Message
Information générale
Forum:
ASP.NET
Catégorie:
Entity Framework
Versions des environnements
Environment:
VB 9.0
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01622996
Message ID:
01623033
Vues:
99
This message has been marked as the solution to the initial question of the thread.
J'aime (1)
The context is a stateful container. Until your SaveChanges() takes the state will not change.

If you want to clear the validation errors you need to recreate the context as there's no built-in way to reset the context (or CancelChanges()).

In my business object wrapper I have a AbortChanges() method which essentially does this:
        /// <summary>
        /// Cancel Changes on the current connected context
        /// </summary>
        public virtual void AbortChanges()
        {
            // Create a new context instance from scratch
            Context.Dispose();  // close the old context

            // Create a new Context
            CreateContext();
        }
which then internally recreates the context.

You can see the code for this here:

https://github.com/RickStrahl/WestwindToolkit/blob/master/Westwind.Data/EfCodeFirst/EfCodeFirstBusinessBase.cs

Context creation after the initial creation is reasonably fast so it's acceptable to recreate the context.

+++ Rick ---


>Consider this method:
>
>It seems that e.ValidationErrors does not get reset. After multiple catches the previous ierrors remain in the collection.
>
>
>
>        public static void savechanges(d060Entities context,out bool success,out string message)
>        {
>            var s = new StringBuilder();
>            s.Clear();
>            success = true;
>            message = "";
>            try
>            {
>                context.SaveChanges();
>            }
>            catch (DbEntityValidationException ex)
>            {
>                s.AppendLine("DbEntityValidationException.");
>                s.AppendFormat("Errors : {0}", ex.EntityValidationErrors.Count());
>                s.AppendLine();
>                foreach (var e in ex.EntityValidationErrors)
>                {
>                    foreach (var e1 in e.ValidationErrors)
>                    {
>                        s.AppendFormat(e1.ErrorMessage);
>                        s.AppendLine();
>                    }
>                }
>                success = false;
>                }
>            catch (Exception ex)
>            {
>                s.AppendFormat("error : {0}", ex.ToString());
>                success = false;
>            }
>            message = s.ToString();
>        }
>
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform