Abort due to constraint violation PRIMARY KEY must be unique
2010-11-25 10:33 Creative dream 阅读(3361) 评论(2) 编辑 收藏 举报记录SQLite唯一主键的错误及解决方法,错误如标题中所示,违反了主键唯一性约束。
- 开发工具:VS 2010 EN
- 开发语言:Visual C#
- SQLite 3
- ADO.NET Entitry Framework
- ADO.NET 2.0 Provider for SQLite
简单设计表结构如下(Students):
Id:integer primary key
在执行
SchoolEntities school = new SchoolEntities(); school.Students.AddObject(new Student { Name = "Tom" }); school.Students.AddObject(new Student { Name = "Jane" }); school.Students.AddObject(new Student { Name = "Rose" }); school.Students.AddObject(new Student { Name = "Maya" }); school.Students.AddObject(new Student { Name = "Maly" }); school.Students.AddObject(new Student { Name = "Marry" }); school.Students.AddObject(new Student { Name = "King" }); school.Students.AddObject(new Student { Name = "Jill" }); school.SaveChanges();
但在执行的时候出错以下错误:
解决方法如下:
1. 为每一个实体的Id提供不同的值
2. 在创建Id列时候设置为:INTEGER PRIMARY KEY AUTOINCREMENT
3. 手动更改Edmx(SSDL)文件,设置为:
<Property Name="Id" Type="integer" Nullable="false" StoreGeneratedPattern="Identity" />