[英]How do I get Dapper.Rainbow to insert to a table with AutoIncrement on SQLite?
I created a sample table on SQLite that has an Id column that is auto increment. 我在SQLite上创建了一个示例表,该表具有一个自动递增的Id列。
CREATE TABLE "ESVLIntegration" ("Id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , "ProcessId" TEXT NOT NULL , "UserId" INTEGER NOT NULL , "Status" TEXT NOT NULL , "StartDate" DATETIME NOT NULL , "EndDate" DATETIME, "Operation" TEXT NOT NULL , "SNEquip" TEXT NOT NULL , "CardName" TEXT NOT NULL , "FilePath" TEXT NOT NULL , "Processed" BOOL NOT NULL )
But when I try to insert for the second time, I get the following error: 但是当我尝试第二次插入时,出现以下错误:
Abort due to constraint violation PRIMARY KEY must be unique 由于违反约束而中止PRIMARY KEY必须是唯一的
This is my code 这是我的代码
public class ESVLIntegration
{
public long Id { get; set; }
public String ProcessId { get; set; }
public long UserId { get; set; }
public String Status { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public String Operation { get; set; }
public String SNEquip { get; set; }
public String CardName { get; set; }
public String FilePath { get; set; }
public Boolean Processed { get; set; }
}
public class Sample : Database<Sample>
{
public Table<ESVLIntegration> ESVLIntegration { get; set; }
}
private void WriteParameters()
{
"Writing sample parameters to SQLite DB".LogDebug();
var pars = new ESVLIntegration();
pars.ProcessId = Guid.NewGuid().ToString();
pars.CardName = "gpp3";
pars.StartDate = DateTime.Now;
pars.Status = "Start";
pars.Operation = VerifyStatus;
pars.SNEquip = "12345";
pars.FilePath = @"C:\Folder\FilePath";
pars.Processed = false;
using (var conn = new SQLiteConnection(connStr))
{
conn.Open();
var db = Sample.Init(conn, 2);
db.ESVLIntegration.Insert(pars);
}
}
Any ideas on what I'm doing wrong here ? 对我在这里做错的任何想法吗?
EDIT 编辑
INTEGER columns on SQlite are of type int64(long) SQlite上的INTEGER列的类型为int64(long)
From the SQLite FAQ I found : 从SQLite FAQ中,我发现:
With this table, the statement 有了这个表,语句
INSERT INTO t1 VALUES(NULL,123); 插入t1值(NULL,123);
is logically equivalent to saying: 从逻辑上讲等同于说:
INSERT INTO t1 VALUES((SELECT max(a) FROM t1)+1,123); 插入t1值((从t1选择max(a))+ 1,123);
So I just changed my class Id to be nullable 所以我只是将我的班级ID更改为可为空
public class ESVLIntegration
{
public long? Id { get; set; }
public String ProcessId { get; set; }
public long UserId { get; set; }
public String Status { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public String Operation { get; set; }
public String SNEquip { get; set; }
public String CardName { get; set; }
public String FilePath { get; set; }
public Boolean Processed { get; set; }
}
Now it works great! 现在效果很好!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.