locked
DBUpdateException in LINQ to Entity Framework RRS feed

  • Question

  • Hi

    I'm using linq to entity framework to insert rows on sql server database , i have created constraint on a destination table on database , one of the inserted rows conflict with this constraint and raise an error on my application , 

    the question is that i want to specify which of inserted row are conflicted with the database constraint ?

    Thanks for attention 

    Tuesday, February 11, 2014 6:50 AM

Answers

  • Hello,

    >>the question is that i want to specify which of inserted row are conflicted with the database constraint ?

    Have a try to use codes like below, if I insert a exist record into database, it will throw the conflict record id as I set.

    using (DataBaseFirstDBEntities db = new DataBaseFirstDBEntities())
    
                {
    
                    try
    
                    {
    
                        //Insert Conflict Record
    
                        Order order = new Order() { OrderID = 1, OrderCode = "1", OrderName = "1" };
    
                        db.Orders.Add(order);
    
                        db.SaveChanges();
    
                    }
    
                    catch (Exception ex)
    
                    {
    
                        IEnumerable<DbEntityEntry> Entries = ((DbUpdateException)ex).Entries;
    
                        foreach (DbEntityEntry entity in Entries)
    
                        {
    
                            Console.WriteLine("The record id of conflict row is " + ((Order)entity.Entity).OrderID);
    
                        }
    
                    }
    
                }
    

    The result:

    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.

    • Marked as answer by Bin Ga3fer Wednesday, February 12, 2014 7:23 AM
    Wednesday, February 12, 2014 6:49 AM
  • Hello,

    >>previous way of handling exception dosn't exactly specify which row have conflict 

    This is strange because I make a test which is similar with yours:

    using (DataBaseFirstDBEntities db = new DataBaseFirstDBEntities())
                {
                    try
                    {
                        //Insert Conflict Record
    
                        for (int i = 0; i < 5; i++)
                        {
                            db.Orders.Add(new Order() { OrderID = i, OrderCode = "" + i + "", OrderName = "" + i + "" });
                        }
                        db.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        IEnumerable<DbEntityEntry> Entries = ((DbUpdateException)ex).Entries;
                        foreach (DbEntityEntry entity in Entries)
                        {
                            Console.WriteLine("The record id of conflict row is " + ((Order)entity.Entity).OrderID);
                        }
                    }
                }

     It could exactly specify which row have conflict:

    Could you please upload your program to skydriver so that we can download it and test it to check why it dose not specify which row have conflict.

    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.

    • Marked as answer by Bin Ga3fer Thursday, February 13, 2014 6:26 AM
    Wednesday, February 12, 2014 9:08 AM

All replies

  • Hi Bin,

    I think this thread is specific to LINQ to Entity forum. So I am helping you to move this thread directly into that forum. Thank you for your understanding.

    By the way, it can help us to understand you question more easily if you provide some code.

    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, February 12, 2014 3:11 AM
  • Hello,

    >>the question is that i want to specify which of inserted row are conflicted with the database constraint ?

    Have a try to use codes like below, if I insert a exist record into database, it will throw the conflict record id as I set.

    using (DataBaseFirstDBEntities db = new DataBaseFirstDBEntities())
    
                {
    
                    try
    
                    {
    
                        //Insert Conflict Record
    
                        Order order = new Order() { OrderID = 1, OrderCode = "1", OrderName = "1" };
    
                        db.Orders.Add(order);
    
                        db.SaveChanges();
    
                    }
    
                    catch (Exception ex)
    
                    {
    
                        IEnumerable<DbEntityEntry> Entries = ((DbUpdateException)ex).Entries;
    
                        foreach (DbEntityEntry entity in Entries)
    
                        {
    
                            Console.WriteLine("The record id of conflict row is " + ((Order)entity.Entity).OrderID);
    
                        }
    
                    }
    
                }
    

    The result:

    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.

    • Marked as answer by Bin Ga3fer Wednesday, February 12, 2014 7:23 AM
    Wednesday, February 12, 2014 6:49 AM
  • Thanks too much
    Wednesday, February 12, 2014 7:01 AM
  • but if i have multiple row to insert into database at the same entity , previous way of handling exception dosn't exactly specify which row have conflict 

    my code id 

     

    foreach (DataRow row in invoiceTable.Rows)
                            {
                                db.invoice.Add(new invoice()
                                {
                                    itemid = Convert.ToInt32(row["ItemID"]),
                                    orderid = si.orderid,
                                    qty = Convert.ToDouble(row["QTY"]),
                                    price = Convert.ToDouble(row["Price"])
                                });
                            }

    Context.SaveChanges();


    Wednesday, February 12, 2014 8:46 AM
  • Hello,

    >>previous way of handling exception dosn't exactly specify which row have conflict 

    This is strange because I make a test which is similar with yours:

    using (DataBaseFirstDBEntities db = new DataBaseFirstDBEntities())
                {
                    try
                    {
                        //Insert Conflict Record
    
                        for (int i = 0; i < 5; i++)
                        {
                            db.Orders.Add(new Order() { OrderID = i, OrderCode = "" + i + "", OrderName = "" + i + "" });
                        }
                        db.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        IEnumerable<DbEntityEntry> Entries = ((DbUpdateException)ex).Entries;
                        foreach (DbEntityEntry entity in Entries)
                        {
                            Console.WriteLine("The record id of conflict row is " + ((Order)entity.Entity).OrderID);
                        }
                    }
                }

     It could exactly specify which row have conflict:

    Could you please upload your program to skydriver so that we can download it and test it to check why it dose not specify which row have conflict.

    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.

    • Marked as answer by Bin Ga3fer Thursday, February 13, 2014 6:26 AM
    Wednesday, February 12, 2014 9:08 AM
  • i'm sorry this way is that what i search for , thanks for your reply
    Wednesday, February 12, 2014 9:35 AM
  • sorry if there is a solution for my special case .

    I have created a linq query that used to inset data into 'Production'  table with the database
    this table has trigger that fired on insert ,this trigger used to insert some data into 'TransactionHistory' table ,

    --after each production insert the trigger will insert to TransactionHistory table the produced Item and the consumed materials , 
    but in some cases conflict with akey is occured when trying inseting non balance covered materials 

    i want to pick this itemid by using linq query ,

    Note : 

    each material item has an itemid , the balance column of transactionHistory has check constraint Balance>=0

    so if any type of material will be inserted with balance less than 0 it will make a conflict , so i want to know what is the conflicted itemid ?

    Thanks for attention


    Tuesday, July 15, 2014 11:02 PM
  • sorry if there is a solution for my special case .

    I have created a linq query that used to inset data into 'Production'  table with the database
    this table has trigger that fired on insert ,this trigger used to insert some data into 'TransactionHistory' table ,

    --after each production insert the trigger will insert to TransactionHistory table the produced Item and the consumed materials , 
    but in some cases conflict with akey is occured when trying inseting non balance covered materials 

    i want to pick this itemid by using linq query ,

    Note : 

    each material item has an itemid , the balance column of transactionHistory has check constraint Balance>=0

    so if any type of material will be inserted with balance less than 0 it will make a conflict , so i want to know what is the conflicted itemid ?

    Thanks for attention



    Tuesday, July 15, 2014 11:02 PM