带实体框架的SQLite [英] SQLite with Entity Framework

查看:98
本文介绍了带实体框架的SQLite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用SQLite时,Entity Framework中的主键出现问题。 SQLite希望在自动递增主键列的VALUES列表中使用显式NULL。我实际上并没有在EF上下文中查看生成的SQL,但是我相信它与通常的SQL Server约定(即不为autoincrementing列提供任何值)一起使用。



根据ADO.NET SQLite提供程序的网站所述,完全支持EF,但我在此找不到任何帮助。有没有一种方法可以强制EF为主键值显式插入NULL?

解决方案

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

 创建表Persona(PersonaID整数主键自动递增,Nombre文本)

此外,我没有找到在Visual Studio中进行设置的方法,我不得不从命令行工具中进行设置。 / p>

更新:以下代码可以正常工作。

  PruebaDBEntities数据= new PruebaDBEntities(); 

foreach(Enumerable.Range(1,1000)中的int num)
{
Persona p = new Persona(){Nombre = Persona + num,Edad = num };

数据。AddToPersona(p);

data.SaveChanges();

Console.WriteLine(p.PersonaID);
}

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


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 haven't actually looked at the generated SQL in the EF Context, but I believe it's going with the usual SQL Server convention of providing no value for the autoincrementing column.

According to the site for the ADO.NET SQLite provider, EF is fully supported but I'm finding no help there. Is there a way to force EF to explicitly insert a NULL for the primary key value?

解决方案

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.

这篇关于带实体框架的SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆