我在用着SQLite
and System.Data.Linq.Mapping
。我有一个问题id
AUTOINCREMENT
使用 linq 映射属性时的字段IsDbGenerated = true
.
创建表的语法。我已经尝试过使用/不使用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,字符串
strSql、SQLiteStatement 先前、UInt32 timeoutMS、String&
strRemain)\r\n 于
System.Data.SQLite.SQLiteCommand.BuildNextCommand()\r\n 位于
System.Data.SQLite.SQLiteCommand.GetStatement(Int32 索引)\r\n 位于
System.Data.SQLite.SQLiteDataReader.NextResult()\r\n 位于
System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd,
CommandBehavior 行为)\r\n at
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 查询信息、IObjectReaderFactory 工厂、Object[]
ParentArgs、对象 [] userArgs、ICompiledSubQuery[] 子查询、对象
最后结果)\r\n 于
System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(表达式查询,
QueryInfo[] queryInfos、IObjectReaderFactory 工厂、Object[]
userArguments、ICompiledSubQuery[] 子查询)\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
位于 Program.cs 中的 SqlLinq.Program.Main(String[] args):第 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