【发布时间】:2015-10-18 09:18:47
【问题描述】:
我正在使用SQLite 和System.Data.Linq.Mapping。我在使用 linq 映射属性 IsDbGenerated = true 时遇到了 id AUTOINCREMENT 字段的问题。
创建表的语法。我试过这个有/没有AUTOINCREMENT
CREATE TABLE [TestTable] ([id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,[title] TEXT NULL)
我的表类:
[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 逻辑错误或缺少数据库\r\nnear \"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 at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 索引)\r\n at System.Data.SQLite.SQLiteDataReader.NextResult()\r\n 在 System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior 行为)\r\n 在 System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior 行为)\r\n 在 System.Data.SQLite.SQLiteCommand.ExecuteDbDataReader(CommandBehavior 行为)\r\n 在 System.Data.Common.DbCommand.ExecuteReader()\r\n
在 System.Data.Linq.SqlClient.SqlProvider.Execute(表达式查询, QueryInfo queryInfo, IObjectReaderFactory 工厂, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object 最后一个结果)\r\n 在 System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(表达式查询, QueryInfo[] queryInfos, IObjectReaderFactory 工厂, Object[] userArguments, ICompiledSubQuery[] subQueries)\r\n at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(表达式 查询)\r\n 在 System.Data.Linq.ChangeDirector.StandardChangeDirector.DynamicInsert(TrackedObject 项目)\r\n 在 System.Data.Linq.ChangeDirector.StandardChangeDirector.Insert(TrackedObject 项目)\r\n 在 System.Data.Linq.ChangeProcessor.SubmitChanges(冲突模式 故障模式)\r\n 在 System.Data.Linq.DataContext.SubmitChanges(冲突模式 failureMode)\r\n 在 System.Data.Linq.DataContext.SubmitChanges()\r\n 在 SqlLinq.Program.Main(String[] args) 在 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
【问题讨论】:
标签: c# sql linq sqlite system.data.sqlite