Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Entity Framework ConCurrent Issue

$
0
0

Hi Everyone,

I need to build an application that will have concurrent users access MS SQL records.  I'm using EF 4.3.1 and have an entity call "apps_SystemInfo and one of the properties is call "RowVersion" which of course is a "TimeStamp" and is set to "Fixed" Concurrent Mode. I'm trying to simulate a scenario for a ConCurrent exception but for some reason it is not working.  What I notice is that the second entity statment(ctx1) is being is being save but not the first(ctx). Therefore SMTPPort is being set to 6000 on the sql record.  I need to catch  the concurrent exception and alert the user.  Any help  would be greatly appreciated.

private void UpdateSystemInfo()
        {
              using (AllocationEntities ctx = new AllocationEntities())
                {
                    var d = (from c in ctx.apps_SystemInfo.Where(p => p.ID == 1)
                                        select c).FirstOrDefault();
                    d.SMTPPort = 8000;
                    using (AllocationEntities ctx1 = new AllocationEntities())
                    {
                        var u = (from c in ctx.apps_SystemInfo.Where(p => p.ID == 1)
                                                 select c).FirstOrDefault();
                        u.SMTPPort = 6000;
                        ctx1.SaveChanges();
                        MessageBox.Show("Update inner update");
                    }

                    try
                    {   //This should trigger a ConcurrentException
                        ctx.SaveChanges();
                        MessageBox.Show("System infomation has been updated successfully", "Result");
                    }
                    catch (OptimisticConcurrencyException oCE)
                    {
                        MessageBox.Show(oCE.Message + " The record you're trying to update has change since you view it, the system will update your form with the changes", "ConCurrent Error!");
                        DisplaySystemInfo();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Exception Error");
                    }
                }
         }

Thanks

Anthony



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>