none
Identifying reason for insert failure RRS feed

  • Question

  • I am using DbContext to access a SQL Server 2014 database.  I use the code below to insert a new row into my table.  If the insert fails, I want to distinguish a duplicate key error from other database errors because I need to inform the user that the key already exists.  If the insert does fail, a DbUpdateException is raised.  However, the only way I've found to identify the reason for the failure is to examine the text of the inner exception.  I don't think relying on the text of this exception is a good programming practice.  Is there a way to determine why the insert failed?

    Thanks

    context.Set<Q_SCHEDULE>().Add(newSchedule);
    context.SaveChanges();

    • Moved by Weiwei Cai Tuesday, July 12, 2016 8:22 AM not VS IDE issue
    Monday, July 11, 2016 5:32 PM

Answers

  • Hi paynefully,

    >>Is there a way to determine why the insert failed?

    According to your description, I would suggest that you could check if the value of key is exist in your database before you insert data into your database. like this:

    using (var db = new EFDemoEntities())
                {
                    try
                    {
                        TestDulicateKey tc = new TestDulicateKey() { Id = 1, Name = "Test2" };
                        var result = db.TestDulicateKeys.Find(tc.Id);
                        if(result ==null)
                        { 
                            db.TestDulicateKeys.Add(tc);
                            db.SaveChanges();
                        }
                    }
                    catch (DbUpdateException e)
                    {
                        Console.WriteLine(e.Message);
                        if (e.InnerException.InnerException.Message.Contains("duplicate key"))
                        {
                            throw new Exception("duplicate key");
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
    
                    Console.ReadKey();
                }

    Best regards,

    Cole Wu


    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, July 12, 2016 9:45 AM
    Moderator

All replies

  • Hi paynefully,

    Thank you for your post.

    Since Our forum is discussing about VS general question like how to set/configure Visual Studio and Visual Studio tools.
    As your question is data platform, I will help you move this case to Data Platform Development  >ADO.NET Entity Framework and LINQ to Entities forum for dedicated support

    Thank you for your understanding.

    Best 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, July 12, 2016 8:04 AM
  • Hi paynefully,

    >>Is there a way to determine why the insert failed?

    According to your description, I would suggest that you could check if the value of key is exist in your database before you insert data into your database. like this:

    using (var db = new EFDemoEntities())
                {
                    try
                    {
                        TestDulicateKey tc = new TestDulicateKey() { Id = 1, Name = "Test2" };
                        var result = db.TestDulicateKeys.Find(tc.Id);
                        if(result ==null)
                        { 
                            db.TestDulicateKeys.Add(tc);
                            db.SaveChanges();
                        }
                    }
                    catch (DbUpdateException e)
                    {
                        Console.WriteLine(e.Message);
                        if (e.InnerException.InnerException.Message.Contains("duplicate key"))
                        {
                            throw new Exception("duplicate key");
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
    
                    Console.ReadKey();
                }

    Best regards,

    Cole Wu


    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, July 12, 2016 9:45 AM
    Moderator