none
DbUpdateConcurrencyException on an simple add RRS feed

  • Question

  •  private void saveprojekt()
            {
                if (context.Database.Connection.State != System.Data.ConnectionState.Open)
                {
                    context.Database.Connection.Open();
                }
                
                context.TbProjekte.Add(projekt);
    
    
               
              
                try
                {
                    context.SaveChanges();
                }
                catch (DbUpdateConcurrencyException dbexception)
                {
                    foreach (var e in dbexception.Entries)
                    {
                        if (e.State != EntityState.Added)
                        {
                            e.OriginalValues.SetValues(e.GetDatabaseValues());
                        }
                        
                    }
                    context.SaveChanges();
                }
                
                this.Close();
                context.Dispose();
                
            }
    

    This code always throws the DbUpdateConcurrency Exception, albeit I'm only adding this one object. I already tried everything I can think of ( or google suggested ), including putting every thing in a using ( SQL.... context = new ...) phrase. 

    Using an SQL Profiler I found that only the insert-procedure is called. If I call the procedure directly with the same parameters everything works fine.

    Any Ideas what the problem could be?

    I would be very glad about any possible solutions to my problem.


    Monday, November 3, 2014 8:53 AM

Answers

  • Hello sheinekamp,

    >> No I didn't try it without procedures. I thought they are necessary.

    I suggest you could try it so that it would help narrow down this issue.

    >> Can I simply remove the procedure mapping?

    Yes, you could follow steps below:

    -> Open your design windows

    -> Right click the entity you want to remove the mapped SP

    -> Select Table Mapping

    -> Select Map Entities to Functions and delete the selected functions.

    Then the insert/update/delete operations are not mapped to the stored procedure anymore.

    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.

    Tuesday, November 11, 2014 9:44 AM
    Moderator

All replies

  • Hello sheinekamp,

    >>This code always throws the DbUpdateConcurrency Exception, albeit I'm only adding this one object.

    With your provided code, I made a test, however, it could work as expected. The test demo:

    DFDBEntities context = new DFDBEntities();
    
                if (context.Database.Connection.State != System.Data.ConnectionState.Open)
    
                {
    
                    context.Database.Connection.Open();
    
                }
    
                Order order = new Order() { OrderID = 3, OrderName = "3", CustomerID = 1 };
    
                context.Orders.Add(order);
    
                try
    
                {
    
                    context.SaveChanges();
    
                }
    
                catch (DbUpdateConcurrencyException dbexception)
    
                {
    
                    foreach (var e in dbexception.Entries)
    
                    {
    
                        if (e.State != EntityState.Added)
    
                        {
    
                            e.OriginalValues.SetValues(e.GetDatabaseValues());
    
                        }
    
    
                    }
    
                    context.SaveChanges();
    
                }
    
                finally
    
                {
    
                    context.Dispose();
    
                }
    

    The only difference is I declare the context object locality while it seems that you make it globally. Not sure if this is the caused reason, however, you could try with my code.

    >> Using an SQL Profiler I found that only the insert-procedure is called. If I call the procedure directly with the same parameters everything works fine.

    I am confused why the Add() method would call the procedure, is that you map CURD operation to separate procedures each? Have you try to do the insert operation without procedure to check if it works?

    And since it throws an exception, please share the detail message information with us so that we can help you better.

    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.

    Tuesday, November 4, 2014 3:40 AM
    Moderator
  • Hello Fred Bao,

    thank you for your answer! Yes, I already tried using a local context, but without success. 

    No I didn't try it without procedures. I thought they are necessary. Can I simply remove the procedure mapping?

    Regards,
    Monday, November 10, 2014 11:27 AM
  • Hello sheinekamp,

    >> No I didn't try it without procedures. I thought they are necessary.

    I suggest you could try it so that it would help narrow down this issue.

    >> Can I simply remove the procedure mapping?

    Yes, you could follow steps below:

    -> Open your design windows

    -> Right click the entity you want to remove the mapped SP

    -> Select Table Mapping

    -> Select Map Entities to Functions and delete the selected functions.

    Then the insert/update/delete operations are not mapped to the stored procedure anymore.

    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.

    Tuesday, November 11, 2014 9:44 AM
    Moderator