locked
DBcontext.SaveChanges() Method Throwing DbUpdateException,I'm Using Entity With Mysql RRS feed

  • Question

  • User1601010121 posted

    I'm using DataFirst Approach for my EnityFramework to mysql Website, I'm using "EF 5.0" . In mydatabase every Table has Id column which is auto incremented key and non-null-able and primary ,

    While I'm inserting data into database Like This

    lamiEntities1 lam = new lamiEntities1();
                        ordered or = new ordered();
                        or.PartyId = id;
                        or.ItemId = itemid;
                        or.Desc = txtDesc.Text;
                        or.Dated = date;
                        or.Qty =int.Parse( txtqty.Text);
                        or.Remarks = txtremarks.Text;
                        lam.ordereds.Add(or);
                        lam.SaveChanges();

    on Line I get Exception " 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code"

    lam.SaveChanges();
    Saturday, January 23, 2016 12:27 PM

Answers

  • User-271186128 posted

    Hi morfious90,

    I suggest you could check the DataBase Table definition, whether the id is auto incremented, and the others columns definition (the data type and nullable). Then according it to check the values in the insert method.

    If the id column has already set the auto incremented, there is no need to care about the value.

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 25, 2016 8:40 AM
  • User1601010121 posted

    Problem was that,Parent Table has every column null-able except Id .at-least one Column should be non nullable if you are using foreign key. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 30, 2016 7:52 AM

All replies

  • User1633621018 posted

    Hi,

    Did you check the inner exception and what did it say?  Did you check to see if the database was created and that the schema is as you expect?

    Please check again.

    Saturday, January 23, 2016 1:12 PM
  • User1601010121 posted

    What i See is There is Value 0 for id column which is auto incremented and primary key 
    this is inner exception
    {"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Desc, \r\nAmount, \r\nRemarks, \r\nDate) VALUES (\r\n1, \r\n47, \r\n2, \r\n900, \r\n'0', \r\n100, ' at line 6"}

    Saturday, January 23, 2016 1:17 PM
  • User1633621018 posted

    does you id column has this line on top of it. it would be better to see you claSS.

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    Saturday, January 23, 2016 1:33 PM
  • User753101303 posted

    Hi,

    Looks like a conflict with the Desc keyword. You might be able to escape the column name using an attribute or else you'll have to rename this column.

    Saturday, January 23, 2016 1:51 PM
  • User-698989805 posted

    I think, got the solution. Try this:

    int m = Convert.ToInt32(txtqty.Text);

    lamiEntities1 lam = new lamiEntities1(); ordered or = new ordered(); or.PartyId = id; or.ItemId = itemid; or.Desc = txtDesc.Text; or.Dated = DateTime.Now; //The date format should not be null or you did not pass the date properly or.Qty = m; or.Remarks = txtremarks.Text; try { lam.ordereds.Add(or); lam.SaveChanges(); } catch (Exception ex) { ex.ToString(); }

    Sunday, January 24, 2016 8:39 AM
  • User-271186128 posted

    Hi morfious90,

    I suggest you could check the DataBase Table definition, whether the id is auto incremented, and the others columns definition (the data type and nullable). Then according it to check the values in the insert method.

    If the id column has already set the auto incremented, there is no need to care about the value.

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 25, 2016 8:40 AM
  • User1601010121 posted

    Hi Thanks for Replies,

    I dont think this is because of identity Column because i tried ti insert into "Test" Table without identity column But still I'm getting exception

     lamiEntities1 lam = new lamiEntities1();
                        testtab t = new testtab(); //This is Test table
                        t.a = 12;
                        t.b = "tr";
                        t.c = "kanp";
                        t.d = "delh";
    
    
                        try
                        {
                            lam.testtabs.Add(t);
                            lam.SaveChanges();
                        }
                        catch (Exception ex)
                        {
                            ex.ToString();
                        }

    Exception In testTable

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(SELECT
    testtab.a, 
    testtab.b, 
    testtab.c, 
    testtab.d
    FROM testtab AS testtab)(
    ' at line 1


    Here is exception in my "Ordered" Table that i described in my Question .

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Desc, 
    Qty, 
    Dated, 
    Remarks) VALUES (
    1, 
    47, 
    'dsds', 
    111, 
    '2016-01-' at line 4

    I think Problem is with my Mysq.data.Entity Dll Because all I have is
    Entity Framework 5
    Mysql.DotNetConnector 6.3.7

    Mysql.Data V-6.3.7.0
    Mysql.Data.Entity 6.8.7


    I think Mysql.Data.Entity 6.8.7 is not  compatible with Mysql.Data 6.3.7


    Does anyone have Dll for Mysql.Data V-6.3.7.0 and all dependency .... I tried Nuget Package but its giving latest version that i dont need



    Thursday, January 28, 2016 11:14 AM
  • User1601010121 posted

    Problem was that,Parent Table has every column null-able except Id .at-least one Column should be non nullable if you are using foreign key. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 30, 2016 7:52 AM