Error al usar System.Data.Linq.Mapping e incrementar automáticamente la clave primaria en una base de datos sqlite
Estoy usandoSQLite
ySystem.Data.Linq.Mapping
. Tengo un problema con elid
AUTOINCREMENT
campo cuando se usa el atributo de mapeo linqIsDbGenerated = true
.
Sintaxis para crear mi tabla. He intentado esto con / sin elAUTOINCREMENT
CREATE TABLE [TestTable] ([id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,[title] TEXT NULL)
Mi clase de TABLA:
[Table(Name = "TestTable")]
public class TestTable
{
[Column(IsPrimaryKey = true, IsDbGenerated =true)]
public int id { get; set; }
[Column]
public string title { get; set; }
}
Así es como lo llamo. Cuando se envía, recibo un error, pegaré el error debajo de este ejemplo. Una cosa a tener en cuenta es si saco elIsDbGenerated =true
arriba e ingrese elid
manualmente se inserta bien, pero me gustaría queAUTOINCREMENT
y por alguna razón elIsDbGenerated=true
está matando el inserto. Buscando alguna orientación.
static void Main(string[] args)
{
string connectionString = @"DbLinqProvider=Sqlite;Data Source = c:\pathToDB\test.s3db";
SQLiteConnection connection = new SQLiteConnection(connectionString);
DataContext db = new DataContext(connection);
db.Log = new System.IO.StreamWriter(@"c:\pathToDB\mylog.log") { AutoFlush = true };
var com = db.GetTable<TestTable>();
com.InsertOnSubmit(new TestTable {title = "asdf2" });
try {
db.SubmitChanges();
}
catch(SQLiteException e)
{
Console.WriteLine(e.Data.ToString());
Console.WriteLine(e.ErrorCode);
Console.WriteLine(e.HelpLink);
Console.WriteLine(e.InnerException);
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
Console.WriteLine(e.TargetSite);
Console.WriteLine(e.ToString());
}
foreach (var TestTable in com)
{
Console.WriteLine("TestTable: {0} {1}", TestTable.id, TestTable.title);
}
Console.ReadKey();
}
Mensaje de error:
Error de lógica SQL o falta de base de datos \ r \ nnear \ "SELECT \": error de sintaxis
Seguimiento de pila:
en System.Data.SQLite.SQLite3.Prepare (SQLiteConnection cnn, String strSql, SQLiteStatement previous, UInt32 timeoutMS, String & strRemain) \ r \ n at System.Data.SQLite.SQLiteCommand.BuildNextCommand () \ r \ n en System.Data .SQLite.SQLiteCommand.GetStatement (índice Int32) \ r \ n en System.Data.SQLite.SQLiteDataReader.NextResult () \ r \ n en System.Data.SQLite.SQLiteDataReader..ctor (SQLiteCommand cmd, CommandBehavior behave) \ r \ n en System.Data.SQLite.SQLiteCommand.ExecuteReader (comportamiento CommandBehavior) \ r \ n en System.Data.SQLite.SQLiteCommand.ExecuteDbDataReader (comportamiento CommandBehavior) \ r \ n en System.Data.Common.DbCommand.ExecuteReader () \ r \ n
en System.Data.Linq.SqlClient.SqlProvider.Execute (consulta de expresión, QueryInfo queryInfo, fábrica IObjectReaderFactory, Object [] parentArgs, Object [] userArgs, ICompiledSubQuery [] subQueries, Object lastResult) \ r \ n en System.Data.Linq .SqlClient. .IProvider.Execute (consulta de expresión) \ r \ n en System.Data.Linq.ChangeDirector.StandardChangeDirector.DynamicInsert (elemento TrackedObject) \ r \ n en System.Data.Linq.ChangeDirector.StandardChangeDirector.Insert (elemento TrackedObject) \ r \ n en System.Data.Linq.ChangeProcessor.SubmitChanges (ConflictMode failureMode) \ r \ n en System.Data.Linq.DataContext.SubmitChanges (ConflictMode failureMode) \ r \ n en System.Data.Linq.DataContext.SubmitChanges () \ r \ n en SqlLinq.Program.Main (String [] args) en Program.cs: línea 29 "
Esto es lo que estoy viendo en la salida del registro:
INSERT INTO [company]([title])
VALUES (@p0)
SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]
-- @p0: Input String (Size = 4000; Prec = 0; Scale = 0) [asdf2]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.6.81.0
SELECT [t0].[id], [t0].[title]
FROM [company] AS [t0]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.6.81.0