SQLite with Entity Framework

后端 未结 6 2157
闹比i
闹比i 2021-01-30 17:36

I\'m having a problem with primary keys in Entity Framework when using SQLite. SQLite wants an explicit NULL in the VALUES list on an autoincrementing primary key column. I ha

6条回答
  •  天命终不由人
    2021-01-30 18:27

    Well I've finally made this work :D. You need to set the id column as autoincrement, this way it does work with EF. Dont ask me why this isnt mentioned in the question about auto-increment in sqlite faq. This is an example:

    create table Persona ( PersonaID integer primary key autoincrement, Nombre text)
    

    Also, I didn't found a way to set this from within visual studio, I had to do it from the command line tool.

    UPDATE: The following code works fine.

    PruebaDBEntities data = new PruebaDBEntities();
    
            foreach (int num in Enumerable.Range(1, 1000))
            {
                Persona p = new Persona() { Nombre = "Persona " + num, Edad = num };
    
                data.AddToPersona(p);
    
                data.SaveChanges();
    
                Console.WriteLine(p.PersonaID);
            }
    

    The PersonaID wasn't set, and after the save operation it had the value asigned by sqlite.

提交回复
热议问题