2012-08-02 12 views
5

Ho creato una tabella di esempio su SQLite che ha una colonna Id che è l'incremento automatico.Come si ottiene Dapper.Rainbow da inserire in una tabella con AutoIncrement su SQLite?

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) 

Ma quando provo ad inserire per la seconda volta, ottengo il seguente errore:

Abort due to constraint violation PRIMARY KEY must be unique

Questo è il mio codice

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); 
    } 
} 

tutte le idee su quello che sto facendo sbagliato qui?

EDIT

colonne INTEGER su SQLite sono di int64 di tipo (lunghe)

+0

hai bisogno di hackerare la fonte dell'arcobaleno per farlo funzionare –

+0

Ho capito che funziona bene come da mia risposta. L'impostazione del campo chiave come nullable ha fatto il trucco. –

risposta

5

Dalla FAQ SQLite ho trovato:

With this table, the statement

INSERT INTO t1 VALUES(NULL,123);

is logically equivalent to saying:

INSERT INTO t1 VALUES((SELECT max(a) FROM t1)+1,123);

Così ho appena cambiato la mia classe Id essere nullable

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; } 
} 

Ora funziona alla grande!

Problemi correlati