Sto utilizzando SQLite
e System.Data.Linq.Mapping
. Sto riscontrando un problema con il campo AUTOINCREMENT
quando si utilizza l'attributo di mappatura linq IsDbGenerated = true
.Errore nell'uso di System.Data.Linq.Mapping e incremento automatico della chiave primaria in un dl sqlite
Sintassi per creare il mio tavolo. Ho provato questo con/senza la AUTOINCREMENT
CREATE TABLE [TestTable] ([id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,[title] TEXT NULL)
La mia classe TABELLA:
[Table(Name = "TestTable")]
public class TestTable
{
[Column(IsPrimaryKey = true, IsDbGenerated =true)]
public int id { get; set; }
[Column]
public string title { get; set; }
}
Ecco come lo chiamo io. Quando si passa a submit ottengo un errore, incollo l'errore sotto questo esempio. Una cosa da notare è che se tolgo lo IsDbGenerated =true
sopra e inserisco manualmente lo id
inserisco bene, ma mi piacerebbe farlo a AUTOINCREMENT
e per qualche motivo lo IsDbGenerated=true
sta uccidendo l'inserto. Alla ricerca di una guida.
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();
}
Messaggio di errore:
errore logico SQL o mancante del database \ r \ nnear \ "SELEZIONE \": errore di sintassi
traccia stack:
a System.Data.SQLite.SQLite3.Prepare (SQLiteConnection cnn, String strSql, SQLiteStatement precedente, Timeout UInt32 MS, String & strRemain) \ r \ n al System.Data.SQLite.SQLiteCommand.BuildNextCommand() \ r \ n al System.Data.SQLite.SQLiteCommand.GetStatement (indice Int32) \ r \ n al sistema .Data.SQLite.SQLiteDataReader.NextResult() \ r \ n a System.Data.SQLite.SQLiteDataReader..ctor (SQLiteCommand cmd, CommandBehavior si comportano) \ r \ n a System.Data.SQLite.SQLiteCommand.ExecuteReader (CommandBehavior comportamento) \ r \ n al System.Data.SQLite.SQLiteCommand.ExecuteDbDataReader (CommandBehavior comportamento) \ r \ n al System.Data.Common.DbCommand.ExecuteReader() \ r \ n
a System.Data .Linq.SqlClient.SqlProvider.Execute (query di espressione, QueryInfo QueryInfo, IObjectReaderFactory fabbrica, oggetto [] parentArgs, Object [] userArgs, ICompiledSubQuery [] Subquery, Object LASTRESULT) \ r \ n al System.Data.Linq.SqlClient.SqlProvider.ExecuteAll (query Espressione, QueryInfo [] queryInfos, IObjectReaderFactory factory, Object [] userArguments, ICompiledSubQuery [] subQueries) \ r \ n a System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute (Expression query) \ r \ n al System.Data.Linq.ChangeDirector.StandardChangeDirector.DynamicInsert (TrackedObject articolo) \ r \ n al System.Data.Linq.ChangeDirector.StandardChangeDirector.Insert (TrackedObject ite m) \ r \ n al System.Data.Linq.ChangeProcessor.SubmitChanges (ConflictMode failureMode) \ r \ n al System.Data.Linq.DataContext.SubmitChanges (ConflictMode failureMode) \ r \ n al System.Data .Linq.DataContext.SubmitChanges() \ r \ n a SqlLinq.Program.Main (String [] args) nel programma.cs: linea 29"
Ecco quello che sto vedendo in uscita 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
Ho provato in questo modo solo ora. Ho ricreato la tabella con il tuo esempio SQL e rimosso la proprietà IsDbGenerated.Esso entra nel primo record aggiungendo uno 0 per l'ID. Il secondo tentativo fallisce con lo stesso errore come la mia descrizione Questo è lo stesso comportamento che stavo vedendo prima. – Terco
Cambia il tuo id in 'Nullable' in questo modo:' public int? id {get; set;} ' –