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:
01623008
Vues:
43
I tested on 1 one table, without any transcaction. Same problem.

>Yesterday I wrote a stored procedure for similar requirement passing my rows as Table Valued parameters. I did have to convert classes to DataTable, though.
>
Untyped DataTable?

>
>>>I think you're over-complicating the process. You can call SaveChanges method only once after all processing is done. It will save all the changes at once.
>>
>>I need the multiple SaveChanges to get the keys that are created and pass them to the other table(s), no? I wouldn't if I had set foreign keys, but I haven't because this is not really applying (as I think you noticed before).
>>
>>The exception is thrown after the first SaveChanges anyway, because the field CalculationStatus is not set, although it is required.
>>
>>Then the transcation is rolled back and the processRow method is exited, and the next row is processed, beginning a new transaction. Each row is processed during one transaction.
>>
>>>
>>>It is probably what it's doing anyway since you opened transaction. So, it's saving all the errors. You can probably explicitly clear that collection if it allows to clear it.
>>>
>>>>I'm calling this method for a number of different rows :
>>>>
>>>>
>>>>       private void process()
>>>>        {
>>>>            if (InputBox.confirm("The database will be intialized.")!=System.Windows.Forms.DialogResult.OK) { return; };
>>>>            Monitor.write("Starting Process : Begin Inventory");
>>>>            _interrupt = false;
>>>>            Dispatcher.BeginInvoke(new ThreadStart(() => Statusbar.displayFirstField("Processing...")));
>>>>            using (var xl = new ExcelWrapper()) using (var context = new d060Entities())
>>>>            {
>>>>                var bis = new BeginInventorySource(xl);
>>>>                var tm = new TimeMonitor(2, bis.LastRow);
>>>>                Monitor.write("Lines to process : {0}", bis.LastRow - 1);
>>>>                for  (long r = 2;r<=bis.LastRow;r++)
>>>>                {
>>>>                    if (_interrupt) { return; };
>>>>                    processRow(r, context, bis,tm);
>>>>
>>>>                }
>>>>            }       
>>>>        }
>>>>        void processRow(long r, d060Entities context,BeginInventorySource bis, TimeMonitor tm)
>>>>        {
>>>>            string d020Parcel;
>>>>            string goods;
>>>>            decimal weight;
>>>>            decimal amount;
>>>>            bis.getRow(r, out d020Parcel, out goods, out weight, out amount);
>>>>            var statusMessage = string.Format("Processing... {0} on row {1} {2}", d020Parcel, r, tm.display(r));
>>>>            Dispatcher.BeginInvoke(new ThreadStart(() => Statusbar.displayFirstField(statusMessage)));
>>>>            var parcel = new d060Entity.Parcel();
>>>>            parcel.Cost = amount;
>>>>            parcel.ParcelWeight = weight;
>>>>            parcel.ParcelStatus = d060Entity.Repository.getParcelstatus(d060Entity.Repository.eParcelstatus.Active);
>>>>            parcel.Creationdate = DateTime.Now;
>>>>            parcel.Lastupdatedate = DateTime.Now;
>>>>            parcel.Goods = goods;
>>>>            parcel.D020number = d020Parcel;
>>>>            parcel.D020wrapper = "";
>>>>            // parcel.CalculationStatus = "Locked"; <== missing assignment
>>>>            var transaction = new d060Entity.Transaction();
>>>>            transaction.Creationdate = DateTime.Now;
>>>>            transaction.Lastupdatedate = DateTime.Now;
>>>>            transaction.TransactionDate = new DateTime(2014, 1, 1);
>>>>            transaction.TransactionWeight = weight;
>>>>            transaction.Amount = amount;
>>>>            transaction.Transactiontypes_id = d060Entity.Repository.getTransactiontypes_id(d060Entity.Repository.eTransactiontypes.BeginInventory);
>>>>            transaction.Goods = goods;
>>>>            transaction.D010Transaction = "";
>>>>            transaction.D010Transactiontype = " ";
>>>>            transaction.D020Transaction = "";
>>>>            transaction.D020Transactiontype = " ";
>>>>            var origin = new d060Entity.Origin();
>>>>            origin.Parcels_id = parcel.Parcels_Id;
>>>>            origin.OriginCalculationstatus = "Locked";
>>>>            {
>>>>                using (var t = context.Database.BeginTransaction())
>>>>                {
>>>>                    try
>>>>                    {
>>>>                        string message;
>>>>                        bool success;
>>>>                        context.Parcels.Add(parcel);
>>>>                        Repository.savechanges(context, out success, out message);
>>>>                        if (success == false)
>>>>                        {
>>>>                            Monitor.write("**** Currently processing : {0} **** ", parcel.D020number);
>>>>                            Monitor.write(message);
>>>>                            t.Rollback();
>>>>                            return;
>>>>                        };
>>>>                        transaction.Parcels_Id = parcel.Parcels_Id;
>>>>                        context.Transactions.Add(transaction);
>>>>                        Repository.savechanges(context, out success, out message);
>>>>                        if (success == false)
>>>>                        {
>>>>                            Monitor.write("**** Currently processing : {0} **** ", parcel.D020number);
>>>>                            Monitor.write(message);
>>>>                            t.Rollback();
>>>>                            return;
>>>>                        };
>>>>                        parcel.CreationTransaction_Id = transaction.Transactions_Id;
>>>>                        origin.Parcels_id = parcel.Parcels_Id;
>>>>                        context.Origins.Add(origin);
>>>>                        Repository.savechanges(context, out success, out message);
>>>>                        if (success == false)
>>>>                        {
>>>>                            Monitor.write("**** Currently processing : {0} **** ", parcel.D020number);
>>>>                            Monitor.write(message);
>>>>                            t.Rollback();
>>>>                            return;
>>>>                        };
>>>>                        t.Commit();
>>>>                    }
>>>>                    catch (Exception ex)
>>>>                    {
>>>>                        Monitor.write("Problem with Parcel {0}, line {1} : ", parcel.D020number, r, ex.Message);
>>>>                        t.Rollback();
>>>>                    }
>>>>                }
>>>>            }
>>>>        }
>>>>
>>>>
>>>>>Hmm, never noticed this problem before. Are you calling this method for a number of rows all having the same problem or it's just a single row save and every time it increments?
>>>>>
>>>>>>Right. Sorry.
>>>>>>
>>>>>>Maybe if I show you the result ?
>>>>>>
>>>>>>8/6/2015 4:37:41 PM : **** Currently processing : A$$00100 ****
>>>>>>8/6/2015 4:37:41 PM : DbEntityValidationException.
>>>>>>Errors : 1
>>>>>>The CalculationStatus field is required.
>>>>>>
>>>>>>8/6/2015 4:37:41 PM : **** Currently processing : A$$00124 ****
>>>>>>8/6/2015 4:37:41 PM : DbEntityValidationException.
>>>>>>Errors : 2
>>>>>>The CalculationStatus field is required.
>>>>>>The CalculationStatus field is required.
>>>>>>
>>>>>>8/6/2015 4:37:41 PM : **** Currently processing : A$$00125 ****
>>>>>>8/6/2015 4:37:41 PM : DbEntityValidationException.
>>>>>>Errors : 3
>>>>>>The CalculationStatus field is required.
>>>>>>The CalculationStatus field is required.
>>>>>>The CalculationStatus field is required.
>>>>>>
>>>>>>8/6/2015 4:37:41 PM : **** Currently processing : A$$00215 ****
>>>>>>8/6/2015 4:37:41 PM : DbEntityValidationException.
>>>>>>Errors : 4
>>>>>>The CalculationStatus field is required.
>>>>>>The CalculationStatus field is required.
>>>>>>The CalculationStatus field is required.
>>>>>>The CalculationStatus field is required.
>>>>>>
>>>>>>8/6/2015 4:37:41 PM : **** Currently processing : A$$00344 ****
>>>>>>8/6/2015 4:37:41 PM : DbEntityValidationException.
>>>>>>Errors : 5
>>>>>>The CalculationStatus field is required.
>>>>>>The CalculationStatus field is required.
>>>>>>The CalculationStatus field is required.
>>>>>>The CalculationStatus field is required.
>>>>>>The CalculationStatus field is required.
>>>>>>
>>>>>>
>>>>>>>>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();
>>>>>>>>        }
>>>>>>>>
>>>>>>>
>>>>>>>Your question is unclear. The code seems to be OK, I see no problem.

If things have the tendency to go your way, do not worry. It won't last. Jules Renard.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform