in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

LINQ, EF: Inserting rows via SaveChanges() not working

Last post 08-23-2010 10:02 PM by tarakanoff. 10 replies.
Page 1 of 1 (11 items)
Sort Posts: Previous Next
  • 08-22-2008 9:18 AM

    LINQ, EF: Inserting rows via SaveChanges() not working

    Hi all,

    I'm currently trying out the LINQ to SQLite and Entitiy Framework stuff in C#, and it seems to fit my needs perfectly. However, I'm having trouble inserting new rows into my tables.

    I created a test table via the sqlite3 command line tool using this SQL string: "CREATE TABLE Test ( a INT, b INT, PRIMARY KEY (a) );" After generating the edmx, I tried this code to insert a row:

    db = new testEntities();
    Test x = new Test();
    x.a = 2; x.b = 3;
    db.AddToTest(x);
    db.SaveChanges();

    The last statement always throws a System.Data.OptimisticConcurrencyException and no rows are actually inserted. I can view data from the table just fine, using the LINQ query "from t in db.Test select t;". It's just SaveChanges() that doesn't work.

    I compiled and ran the testlinq test suite from the source distribution and it worked. Also, I can insert rows into northwind.db in my own code. So did I maybe create my db file the wrong way?

    Any help you be appreciated :)

    Filed under:
  • 08-22-2008 9:27 AM In reply to

    Re: LINQ, EF: Inserting rows via SaveChanges() not working

    Ok I'll have a look.  You're using the RTM, right?

  • 08-22-2008 11:01 AM In reply to

    Re: LINQ, EF: Inserting rows via SaveChanges() not working

     Thanks. I have "Version 9.0.30729.1 SP", no Express edition or beta, if that's what you're asking.

  • 08-24-2008 10:18 PM In reply to

    Re: LINQ, EF: Inserting rows via SaveChanges() not working

    Hi!

    I have the same problem.

    The error is :

    System.Data.SQLite.SQLiteException: Abort due to constraint violation
        Author.ID may not be NULL

     

    My test table is:

    CREATE TABLE [Author](   

        [ID] integer primary key,
        [AuthorName] nvarchar(20) NOT NULL

    );

     My code is :

                SQLiteEntities context = new SQLiteEntities();

                 Author newAuthor = new Author()
                {
                    ID = 1,
                    AuthorName= "tony"
                };

                context.AddToAuthor(newAuthor);
                context.SaveChanges();

     

    But,when my table modifid to following,this code runned right:

     

    CREATE TABLE [Author](   

        [ID] integer primary key autoincrement,
        [AuthorName] nvarchar(20) NOT NULL

    );

     

    why?

  • 08-25-2008 5:00 AM In reply to

    Re: LINQ, EF: Inserting rows via SaveChanges() not working

    Hi,

     I have the same problem. I have made an example in the testlinq source:

    Customers cust = new Customers();
    cust.CustomerID =
    "MTMTM";
    cust.ContactName =
    "My Name";
    cust.CompanyName =
    "SQLite Company";
    cust.Country =
    "Netherlands";
    cust.City =
    "Amsterdam";
    cust.Phone =
    "012345677";
    db.AddToCustomers(cust);
    db.SaveChanges();

     

    This is failing. It has something to do with the key value. Hopefully you can solve this quickly (I was already using this for a customer :) oops, becuase I really love to use this).

     

  • 08-25-2008 5:09 AM In reply to

    Re: LINQ, EF: Inserting rows via SaveChanges() not working

    Another example is:

    Table:

    CREATE TABLE [Countries] (
    [CountryID] integer NOT NULL,
    [CountryLong] nvarchar(255)  NULL,
    [CountryShort] nvarchar(3) NULL,
     CONSTRAINT [PK_CountryID] PRIMARY KEY( [CountryID] ASC)
    )

            Using db As DBModel.DBEntities = New DBModel.POIDBEntities
                db.AddToCountries(New DBModel.Countries With {.CountryID = 100,.POICountryLong = "The Netherlands", .POICountryShort = "NL"})
                db.SaveChanges()
            End Using

     

    for some reason the CountryID is not used! Another ID will be put into the database (1).

    Marcel

    (PS Is there a way to see the SQL string before executing this so that I can see what is happening?)

    Filed under:
  • 08-25-2008 7:43 AM In reply to

    Re: LINQ, EF: Inserting rows via SaveChanges() not working

    I'll work on this.

    How come CountryID is declared "integer primary key" and you're assigning it a string value of "100"?  Lose the quotes and it might return the value you're expecting.

     

  • 08-25-2008 7:47 AM In reply to

    Re: LINQ, EF: Inserting rows via SaveChanges() not working

    Thanks!

    The "100" was a typo. when I use 100 I have the same results.

  • 08-25-2008 8:46 AM In reply to

    Re: LINQ, EF: Inserting rows via SaveChanges() not working

    This is working in the pending 57 release due out this week, or as soon as the new version of SQLite is out.  Whichever comes first.

  • 05-10-2009 11:53 PM In reply to

    Re: LINQ, EF: Inserting rows via SaveChanges() not working

    I have the same problem like yours before Now I solveed the problem by maketed the ID with " AUTOINCREMENT" CREATE TABLE "Imageser" ( "ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "Category" TEXT, )
    Youngfe from ciico jewelry
  • 08-23-2010 10:02 PM In reply to

    Re: LINQ, EF: Inserting rows via SaveChanges() not working

    Check that the data types in fields of tables conform to the requirements specified in the documentation SQLite (Datatypes In SQLite Version 3 - 2.2 Affinity Name Examples).
Page 1 of 1 (11 items)
Powered by Community Server (Commercial Edition), by Telligent Systems