SQLite与实体框架

Dav*_*sky 26 entity-framework system.data.sqlite

使用SQLite时,我在Entity Framework中遇到主键问题.SQLite希望在自动增量主键列的VALUES列表中显式为NULL.我实际上没有看过EF上下文中生成的SQL,但我相信它与通常的SQL Server约定一致,即不为自动增量列提供任何值.

根据ADO.NET SQLite提供程序的站点,EF完全受支持,但我找不到任何帮助.有没有办法强制EF为主键值显式插入NULL?

Pab*_*ote 18

好吧,我终于完成了这项工作:D.您需要将id列设置为autoincrement,这样它就可以与EF一起使用.不要问我为什么在sqlite faq中关于自动增量的问题中没有提到这一点.这是一个例子:

create table Persona ( PersonaID integer primary key autoincrement, Nombre text)
Run Code Online (Sandbox Code Playgroud)

另外,我没有找到从visual studio中设置它的方法,我必须从命令行工具中完成.

更新:以下代码工作正常.

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);
        }
Run Code Online (Sandbox Code Playgroud)

未设置PersonaID,并且在保存操作之后它具有sqlite指定的值.


小智 13

万岁......我找到了解决方案!

  1. 在create table语句中将ID列声明为INTEGER PRIMARY KEY AUTOINCREMENT.INTEGER PRIMARY KEY无效!
  2. 在Visual Studio中更新您的实体框架模型,将ID列属性StoreGeneratedPattern设置为"Computed"

  • 我刚刚完成了第一步,然后删除并创建了我的EF模型.而已.实际上,StoreGeneratedPattern现在已设置为"Identity".它现在正在运作. (3认同)

Moh*_*yed 5

您必须将autoincrement设置为True.您可以在设计表窗口中单击工具栏中的(管理索引和键)图标直接从VS创建此项,然后更新模型.

图片