Ticket Hash: | bde0571ddf5d42a5210aceaa94ca7ccd0d9fd50b | |||
Title: | EntityFramework SaveChanges() exception as PK not advancing | |||
Status: | Closed | Type: | Incident | |
Severity: | Important | Priority: | Medium | |
Subsystem: | LINQ | Resolution: | Works_As_Designed | |
Last Modified: | 2014-02-06 05:14:49 | |||
Version Found In: | 1.0.90.0 | |||
User Comments: | ||||
anonymous added on 2014-02-05 15:01:38:
(text/x-fossil-plain)
Summary: It seems the PK value may not be advancing on EF .SaveChanges() I'm using Data.SQLLite (just downloaded today (5/Feb/14)) with VisStudio 2012 & An Entity Framework (EF) 4 model. I have a table in my SQLite DB, also created today, with SQlite V3.80.3 CREATE TABLE IF NOT EXISTS Widget ( Id INTEGER NOT NULL PRIMARY KEY, DateCreated DATETIME NOT NULL DEFAULT( DATETIME() ), Code TEXT( 5 ) NULL, Fullname TEXT( 100 ) NOT NULL ) & hence an EF Table called 'Widget' & EntitySet called 'Widgets' I declare a new one, & initialise it: var widget = new Widget(); widget.Fullname = 'Some name'; widget.Code = 'W1'; widget.DateCreated = DateTime.Now; _entities.AddToWidgets( widget ); if I inspect widget with QuickWatch, Id is currently 0, as expected. When I run: _entities.SaveChanges(); it throws with: An error occurred while updating the entries. See the inner exception for details. Inner: constraint failed UNIQUE constraint failed: Widgets.Id as yes, there is 1 row in the Widget table, with Id = 0 Does Data.Sqlite handle advancing the PRIMARY KEY value? Thanks! Craig mistachkin added on 2014-02-05 19:16:19: (text/x-fossil-plain) I think the "Id" column should be PRIMARY KEY AUTOINCREMENT. Please try that. anonymous added on 2014-02-06 03:24:21: (text/x-fossil-plain) Hi I thought "AUTOINCREMENT" wasn't required as INSERT statments not specifying an Id from within sqlite were working fine. eg: INSERT INTO WIDGET( Code, Fullname ) VALUES ( 'W10', 'Widget #10' ) But now I see it makes sense as the value it has on insert is 0, not NULL, because thats what it got when I had: widget = new Widget(); I've added AUTOINCREMENT & all seems OK now. Thanks. Craig |