使用System.Data.Linq.Mapping并在sqlite db中自动递增主键时出错

Ter*_*rco 5 c# sql linq sqlite system.data.sqlite

我正在使用SQLiteSystem.Data.Linq.Mapping.id AUTOINCREMENT使用linq mapping属性时,我遇到了该字段的问题IsDbGenerated = true.

创建表的语法.我已经尝试了这个/没有AUTOINCREMENT

CREATE TABLE [TestTable] ([id] INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,[title] TEXT  NULL)
Run Code Online (Sandbox Code Playgroud)

我的TABLE类:

[Table(Name = "TestTable")]
public class TestTable
{
    [Column(IsPrimaryKey = true, IsDbGenerated =true)]
    public int id { get; set; }

    [Column]
    public string title { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是我如何称呼它.当它提交我得到一个错误,我将粘贴此示例下面的错误.需要注意的一件事是,如果我拿出IsDbGenerated =true上面的内容并id手动输入它确实插入正常,但我喜欢它,AUTOINCREMENT并且由于某种原因,它IsDbGenerated=true正在杀死插入物.寻求一些指导.

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();
}
Run Code Online (Sandbox Code Playgroud)

错误信息:

SQL逻辑错误或缺少数据库\ r \nnene \"SELECT \":语法错误

堆栈跟踪:

在System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn,String strSql,SQLiteStatement previous,UInt32 timeoutMS,String&strRemain)\ r \n在System.Data.SQLite.SQLiteCommand.BuildNextCommand()\ r \n在System.Data在System.Data.SQLite.SQLiteDataReader..ctor的System.Data.SQLite.SQLiteDataReader.NextResult()\ r \n中的.SQLite.SQLiteCommand.GetStatement(Int32 index)\ r \n(SQLiteCommand cmd,CommandBehavior behave)\ r \n \n位于System.Data.Comite.DbCommand.ExecuteReader()的System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)\ r \n at System.Data.SQLite.SQLiteCommand.ExecuteDbDataReader(CommandBehavior behavior)\ r \n\r \n
在System.Data.Linq.SqlClient.SqlProvider.Execute(表达式查询,QueryInfo queryInfo,IObjectReaderFactory工厂,Object [] parentArgs,Object [] userArgs,ICompiledSubQuery [] subQueries,Object lastResult)\ r \n在System.Data.Linq .SqlClient.SqlProvider.ExecuteAll(Expression query,QueryInfo [] queryInfos,IObjectReaderFactory factory,Object [] userArguments,ICompiledSubQuery [] subQueries)\ r \n在System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider .IProvider.Execute(表达式查询)\ r \n在System.Data.Linq.ChangeDirector.StandardChangeDirector.DynamicInsert(TrackedObject item)\ r \n在System.Data.Linq.ChangeDirector.StandardChangeDirector.Insert(TrackedObject item)\ r \n \n位于System.Data.Linq.DataContext的System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)\ r \n.在Program.cs中的SqlLinq.Program.Main(String [] args)的System.Data.Linq.DataContext.SubmitChanges()\ r \n的SubmitChanges(ConflictMode failureMode)\ r \n:第29行"

这是我在日志输出中看到的内容:

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
Run Code Online (Sandbox Code Playgroud)

Sal*_*ari 5

根据SQLite文档(声明INTEGER PRIMARY KEY的列将AUTOINCREMENT.)只需AUTOINCREMENT在表创建中删除,写入integer primary key就足够了.SQLite会自动增加你的ids:

sqlite_cmd.CommandText = "CREATE TABLE [TestTable] ([id] INTEGER PRIMARY KEY NOT NULL , [title] TEXT)";
Run Code Online (Sandbox Code Playgroud)

此外,您不需要IsDbGenerated = true在您的TestTable类中设置,也不需要id手动输入,只需插入即可插入title:

com.InsertOnSubmit(new TestTable { title = "asdf2" });//will automatically increment id.
Run Code Online (Sandbox Code Playgroud)

编辑:TestTable现在应该看起来像这样:

[Table(Name = "TestTable")]
public class TestTable
{
    [Column(IsPrimaryKey = true)]
    public int? id { get; set; }

    [Column]
    public string title { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

SQLite管理器中的结果:

结果