gpt4 book ai didi

c# - 使用 System.Data.Linq.Mapping 并自动递增 sqlite 数据库中的主键时出错

转载 作者:IT王子 更新时间:2023-10-29 06:28:25 27 4
gpt4 key购买 nike

我正在使用 SQLiteSystem.Data.Linq.Mapping。使用 linq 映射属性 IsDbGenerated = true 时,id AUTOINCREMENT 字段出现问题。

创建我的表的语法。我已经尝试过使用/不使用 AUTOINCREMENT

CREATE TABLE [TestTable] ([id] INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,[title] TEXT  NULL)

我的 TABLE 类:

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

[Column]
public string title { get; set; }
}

我是这样调用它的。提交时出现错误,我会将错误粘贴到此示例下方。需要注意的一件事是,如果我取出上面的 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();
}

错误信息:

SQL logic error or missing database\r\nnear \"SELECT\": syntax error

堆栈跟踪:

at 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 at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index)\r\n at System.Data.SQLite.SQLiteDataReader.NextResult()\r\n at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)\r\n at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)\r\n at System.Data.SQLite.SQLiteCommand.ExecuteDbDataReader(CommandBehavior behavior)\r\n at System.Data.Common.DbCommand.ExecuteReader()\r\n
at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)\r\n at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)\r\n at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)\r\n at System.Data.Linq.ChangeDirector.StandardChangeDirector.DynamicInsert(TrackedObject item)\r\n at System.Data.Linq.ChangeDirector.StandardChangeDirector.Insert(TrackedObject item)\r\n at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)\r\n at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)\r\n at System.Data.Linq.DataContext.SubmitChanges()\r\n at SqlLinq.Program.Main(String[] args) in Program.cs:line 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

最佳答案

根据 SQLite 文档 (A column declared INTEGER PRIMARY KEY will AUTOINCREMENT.),只需在您的表创建中删除 AUTOINCREMENT,编写 integer primary key 就足够了。 SQLite 会自动递增您的id:

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

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

com.InsertOnSubmit(new TestTable { title = "asdf2" });//will automatically increment id.

编辑:您的 TestTable 现在应该如下所示:

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

[Column]
public string title { get; set; }
}

SQLite Manager 中的结果:

The result

关于c# - 使用 System.Data.Linq.Mapping 并自动递增 sqlite 数据库中的主键时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31665788/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com