SQLite 与实体框架

发布于 2024-07-21 20:18:27 字数 295 浏览 3 评论 0原文

使用 SQLite 时,我在实体框架中遇到主键问题。 SQLite 希望自动增量主键列上的 VALUES 列表中有一个显式 NULL。 我实际上没有查看 EF Context 中生成的 SQL,但我相信它符合通常的 SQL Server 约定,即不为自动增量列提供任何值。

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

装迷糊 2024-07-28 20:18:27

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

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

另外,我没有找到从 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);
        }

PersonaID 未设置,保存操作后它具有由 sqlite 指定的值。

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.

○愚か者の日 2024-07-28 20:18:27

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

  1. 在创建表语句中将 ID 列声明为 INTEGER PRIMARY KEY AUTOINCRMENT。 整数主键不起作用!
  2. 在 Visual Studio 中更新实体框架模型,将 ID 列属性 StoreGeneratePattern 设置为“已计算”

Hooray... I've found the solution!

  1. Declare the ID column as INTEGER PRIMARY KEY AUTOINCREMENT in your create table statement. INTEGER PRIMARY KEY won't work!
  2. Update your Entity Framework Model in Visual Studio, set the ID column property StoreGeneratedPattern to "Computed"
长发绾君心 2024-07-28 20:18:27

您必须将自动增量设置为True。 您可以直接从 VS 进行此操作,方法是在设计表窗口中单击工具栏中的(管理索引和键)图标,然后更新您的模型。

图片

You must set autoincrement to True. You can make this directly from VS by clicking ( Manage Indexes and Keys ) icon from the tool bar while you are in design table window, then update your Model.

image

忆梦 2024-07-28 20:18:27

我在使用 EF 和 SQLite 时也遇到过同样的问题。
尝试检查第二篇文章:
http://sqlite.phxsoftware.com/forums/p/1418/6162。 aspx

我的问题的原因是自动增量添加到数据库本身,但实体模型没有正确刷新。 因此,刷新后,我的字段看起来像这样:(

<Property Name="ID" Type="integer" Nullable="false" StoreGeneratedPattern="Identity" />

添加了 StoreGeneratePattern="Identity"

在刷新之前(使用旧模型),我只是尝试将 id 属性设置为 0,这有效还有:)

I've had the same problem with EF and SQLite.
Try checking the second post:
http://sqlite.phxsoftware.com/forums/p/1418/6162.aspx

The cause for my problem was that the autoincrement was added to the database itself, but the entity model was not properly refreshed. So after the refresh, my field looked something like this:

<Property Name="ID" Type="integer" Nullable="false" StoreGeneratedPattern="Identity" />

(StoreGeneratedPattern="Identity" was added)

Before the refresh (with the old model), I just tried setting the id property to 0, which worked as well :)

一笔一画续写前缘 2024-07-28 20:18:27

在《SQLite 权威指南》一书中,我在主键约束下读到了以下内容:

“然而,实际上,该列只是 ROWID 的别名。它们都引用相同的值。”

我相信这就是需要在列定义中设置 AUTOINCRMENT 的原因。

From the book, "The Definitive Guide to SQLite" I read the following under primary key constraints:

"In reality, however, this column will simply be an alias for ROWID. They will all refer to the same value."

I believe that is the reason for needing to set AUTOINCREMENT in the column definition.

葬心 2024-07-28 20:18:27

SQLite 的 EF 提供程序似乎没有选择该列作为标识(自动增量)。

首先对于数据库,右键单击 EDMX 设计器中的列并将 StoreGeneeratedPattern 设置为 Identity

It seems EF provider for SQLite does not pick up the column as identity (autoincrement).

For database first, right click the column in EDMX designer and set the StoreGeneratedPattern to Identity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文