SQLite 与实体框架
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
好吧,我终于成功了:D。 您需要将 id 列设置为自动增量,这样它就可以与 EF 一起使用。 不要问我为什么在 sqlite 常见问题解答中有关自动增量的问题中没有提到这一点。 这是一个例子:
另外,我没有找到从 Visual Studio 中设置它的方法,我必须从命令行工具中进行设置。
更新:以下代码工作正常。
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:
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.
The PersonaID wasn't set, and after the save operation it had the value asigned by sqlite.
万岁...我找到了解决方案!
Hooray... I've found the solution!
您必须将自动增量设置为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.
我在使用 EF 和 SQLite 时也遇到过同样的问题。
尝试检查第二篇文章:
http://sqlite.phxsoftware.com/forums/p/1418/6162。 aspx
我的问题的原因是自动增量添加到数据库本身,但实体模型没有正确刷新。 因此,刷新后,我的字段看起来像这样:(
添加了 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:
(StoreGeneratedPattern="Identity" was added)
Before the refresh (with the old model), I just tried setting the id property to 0, which worked as well :)
在《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.
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
toIdentity
.