none
DbUpdateConcurrencyException in simple WinForms Project not thrown! RRS feed

  • Question

  • Hi there,

    i have following problem:

    I got a Winforms application in which i use EF 5.0 and have several tables added from DB to the Model.edmx. I fill the datagrid with the linq query (i use a projection class therefore). For the columns i want to be checked for concurrency i set the "fixed" Propert! I also tried to add a Timestamp Field on the DB (and model).

    My problem now is the concurrency. The DbUpdateConcurrencyException which i try to catch, is not thrown! Only when i fire the SaveChanges() from two instances of the application at exactly the same time. Then i can catch it in debug mode in VS2012.

    My Update method looks as follow:

    try
                        {
                            counterDatensaetze = ctx.SaveChanges();
                            MessageBox.Show("Keine Konflikte: " + counterDatensaetze.ToString() + " Gespeichert!");
                        }
                        catch (DbUpdateConcurrencyException ex)
                        {
                            int rapporteID = 0;
                            foreach (var entry in ex.Entries)
                            {
                                
                                //// CLIENT WINS
                                //entry.OriginalValues.SetValues(entry.GetDatabaseValues());
                                //ctx.SaveChanges(); 
    
                                //// Store/DB Wins
                                entry.OriginalValues.SetValues(entry.GetDatabaseValues());
                                entry.CurrentValues.SetValues(entry.GetDatabaseValues());
                                entry.State = EntityState.Unchanged;
                                DbPropertyValues dbProps = entry.CurrentValues;
                                rapporteID = Convert.ToInt32(dbProps["RapporteId"]);
                            
                                var queryGetRapportByID = (from rapport in ctx.mRapporte where rapport.RapporteId == rapporteID select new {rapport.Kommentar, rapport.Visum1}).ToList();
                                foreach (DataGridViewRow row in dgvTest.Rows)
                                {
                                    if (Convert.ToInt32(row.Cells["RapporteId"].Value) == rapporteID)
                                    {
                                        row.Cells["Kommentar"].Value = queryGetRapportByID[0].Kommentar;
                                        row.Cells["Visum1"].Value = queryGetRapportByID[0].Visum1;
                                    }                        
                                }
                            
                            }
                            MessageBox.Show("Ein oder mehrere Datensätze wurden in der Zwischenzeit von einem anderen Benutzer geändert\n und konnten momentan nicht gespeichert werden.\nDie Daten sind nun neu geladen und können angepasst werden.");
                            
                            
                            
                        }

    For understanding...i am right when i assume that the DbUpdateConcurrencyException is also thrown when user A edits and finishes his action and then user B, five minutes later wants to save his changes ?

    I hope really there is somebody who can give me an advice.

    Tuesday, June 10, 2014 11:46 AM

Answers

  • Hi,

    >> - if i have to set my Columns i want to update to "Fixed" or not?

    You can, but it is recommended to use the timestamp column and set it to Fixed in your model.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Leezer Monday, June 16, 2014 12:52 PM
    Thursday, June 12, 2014 7:46 AM
    Moderator

All replies

  • Hello,

    >> My problem now is the concurrency. The DbUpdateConcurrencyException which i try to catch, is not thrown! Only when i fire the SaveChanges() from two instances of the application at exactly the same time. Then i can catch it in debug mode in VS2012.

    The DbUpdateConcurrencyException is used with Entity Framework version >= 6:

    [This page is specific to the Entity Framework version 6. The latest version is available as the 'Entity Framework' NuGet package. For more information about Entity Framework, see msdn.com/data/ef.]

    See it here:

    http://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.dbupdateconcurrencyexception(v=vs.113).aspx

    With Entity Framework 5, it will run successfully even with setting concurrency to be “Fixed”. For example, with below demo:

    Client originalClient;
    
    
                    //    Create an order 
    
                    using (DFDBEntities db = new DFDBEntities())
    
                    {
    
                        originalClient = new Client() { ClientID = 1, ClientName = 1 };
    
                        db.Clients.Add(originalClient);
    
                        db.SaveChanges();
    
                    }
    
    
                    //    Simulate the modification of the created order by user X, the first man has updated the record
    
                    using (DFDBEntities db = new DFDBEntities())
    
                    {    //    Recreate the order object in order to attach it 
    
                        var client = new Client() { ClientID = originalClient.ClientID, ClientName = originalClient.ClientName, TimeStamp = originalClient.TimeStamp };
    
                        db.Clients.Attach(client);
    
    
                        //    Alter the order 
    
                        client.ClientName = 2;
    
    
                        db.SaveChanges();
    
                    }
    
    
    
                    //    Simulate the modification of the created order by user Y (after user X already modified it) , the second man has altered the original value, this will occur this problem.
    
                    using (DFDBEntities db = new DFDBEntities())
    
                    {    //    Recreate the order in order to attach it 
    
                        var client = new Client() { ClientID = originalClient.ClientID, ClientName = originalClient.ClientName, TimeStamp = originalClient.TimeStamp };
    
                        db.Clients.Attach(client);
    
                        //    Alter the order 
    
                        client.ClientName = 3;
    
                        try
    
                        {
    
                            db.SaveChanges();
    
                        }
    
                        catch (DbUpdateConcurrencyException ex)
    
                        {
    
                            Console.WriteLine("Concurrency exception on " + ex.Entries.First().Entity.GetType().Name);
    
                        }
    
                    }
    

    It will change the ClientName to 3 successfully with Entity Framework 5, however it will raise the exception with Entity Framework 6.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, June 11, 2014 6:22 AM
    Moderator
  • Hello Fred, thanks lot for your reply and your code sample!

    I tried to figure out if with EF 6.1.0 my sample application works, (i Updated to EF 6.1.0) like throwing this exception, bi using two instances of the application. But... it didnt threw... only when i fire the saveChanges at the exactly time.

    So now i wonder,

    - if i have to set my Columns i want to update to "Fixed" or not? and.. - if i need to include a Timestamp Column in my Database and include it into my Model.edmx ?  Im a bit confused now, thats why i write like this.

    Any Help is greatly appreciated

    Edit 1: If i run your code, (i mean Fred Baos from this thread above) in a console app, it works like a charm!

    But in my winforms project (my sample application) the exception is not thrown at all when i open two instances of the program and call savechanges() after each other! I set the property on the entity as "Fixed" which one i want to alter, (in my case the property Comment; entity.Comment = "Some text";

    thanks for any help


    • Edited by Leezer Wednesday, June 11, 2014 11:44 AM
    Wednesday, June 11, 2014 7:31 AM
  • Hi,

    >> - if i have to set my Columns i want to update to "Fixed" or not?

    You can, but it is recommended to use the timestamp column and set it to Fixed in your model.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Leezer Monday, June 16, 2014 12:52 PM
    Thursday, June 12, 2014 7:46 AM
    Moderator