0

My WebAPI is successfully working in localhost (with UI in Angular and With postman as well) but when I am publishing it to the Azure server it starts throwing Optimistic Concurrency Exception when trying to update existing entity. Getting same exception even when I am connecting the Server DB using local visual studio 2019 and testing though postman.

This issue drives me crazy since last 3-4 weeks. I have confirm that each and every properties is binding correctly including foreign keys. And Server database has same table structure with all the key's as localhost.

I am using entity framework 6 with repository pattern, .Net framework 4.8, SQL Server 2017 (tried SQL Server 2014).

What I have tried to resolve issue but did not get luck :

  • Set [ConcurencyCheck] to property
  • make sure that no other thread is trying to change entity Or no other transaction is running
  • disabled foreign keys and just updating single column of table
  • I can able to insert new records in same table
  • published database and API both to different server
  • Try to refresh entity with Client wins (https://learn.microsoft.com/en-gb/ef/ef6/saving/concurrency?redirectedfrom=MSDN) and save changes again

I am out of idea now to resolve this issue. Does anyone face the same issue ? Or any configuration that I am missing or should I try ?

Please don't mark this question as duplicate, since I tried possible all the solutions i found in different threads.

[Edit] Sample Code :

// LeadCustomer is inheriting from baseclass ModelBase which has CreatedById, CreatedDate, ModifiedById, ModifiedDate properties
LeadCustomer leadCustomer = dc.LeadCustomers.Where(c => c.Email.EmailAddress.ToLower().Equals(model.EmailAddress.ToLower())).FirstOrDefault();
    if (leadCustomer == null) { 
        leadCustomer.FirstName = "Viral";
        leadCustomer.LastName = "Maru";
        leadCustomer.Email.EmailAddress = "maru.viral@gmail.com"; 
        leadCustomer.SubscriptionTypeID = SubscriptionTypes.Monthly; 
        dc.LeadCustomers.Add(leadCustomer); 
        dc.SaveChanges(); 
    }
Viral Maru
  • 33
  • 5
  • Without a short sample code that can reliably reproduce this, it is hard for us to guess your real issue here. Please post sample code that produce this error. – Rosdi Kasim Jul 08 '21 at 05:20
  • Your entity has a property marked `ConcurencyCheck` and its values in server and in your code are different. Did you understand this much? Now, try to find out what is causing the values to change. Try logging the marked property values and check if they in fact hold correct values. If they do, then check the db server to see what are the actual parameter values that are sent to the db and compare them. – Mat J Jul 08 '21 at 05:21
  • @RosdiKasim : i just edit the post and provide the same code, it's inserting the entity but when i am trying to update same columns than it's throwing error. note that sample code does not have all the columns. I am thinking this issue as any configuration issue rather than code because i have similar implementation in different project and i is working absolutely fine. – Viral Maru Jul 08 '21 at 05:48
  • "*3-4 weeks.*" and you haven't made an [mcve]? I mean, you cant look at code and wish the bugs away... You need to go back to *first principles* and find the minimal set requirements to make something work, and the minimal set of requirements to reproduce your bug.. In your case can you get anything to work?.. yes great, if you do things without a munted repository does it work? yes great, etc etc etc etc until you reach a point where it doesnt work... Then you have a question and likely a solution – TheGeneral Jul 08 '21 at 05:48
  • @TheGeneral : I changed the DataContext implementation to the repository patterns, also try disable the foreign key's. what i tried other solutions is mentioned in the post. but any of the solution did not work. – Viral Maru Jul 08 '21 at 05:55
  • Unfortunately, its hard to help you because we cant reproduce anything, as there is nothing to reproduce. – TheGeneral Jul 08 '21 at 05:57

0 Answers0