我一直在使用Linq數據映射Ms Access數據庫。我照常創建一個OleDbConnection
,並將其傳遞給DataContext
。使用Access數據庫的Linq數據映射:「在SQL語句末尾缺少分號(;)」。
到目前爲止,這一直很好地工作,從基於複雜查詢的表中檢索數據,甚至關係也能自動填充1-N關係中的子實體列表。
然而,當我嘗試使用下面的代碼中插入數據:
[Table(Name = "test_table")]
public class test_item {
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int field1;
[Column]
public int field2;
}
public void Test() {
Table<test_item> tbl = this.GetTable<test_item>();
test_item x = new test_item();
x.field2 = 1222;
tbl.InsertOnSubmit(x);
this.SubmitChanges();
}
我收到以下錯誤:
"Missing semicolon (;) at end of SQL statement."
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.OleDb.OleDbCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader()
at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
at System.Data.Linq.ChangeDirector.StandardChangeDirector.DynamicInsert(TrackedObject item)
at System.Data.Linq.ChangeDirector.StandardChangeDirector.Insert(TrackedObject item)
at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges()
at MyClass.Test() in C:\...\MyClass.cs:line 123
如果我刪除IsDbGenerated
標誌,它不會崩潰,但在那種情況下,我不得不指定主鍵(x.field1 = 55
),但我希望它自動分配。
如何避免發生此異常?
不幸的是,EF不支持MS Access。 – Igor 2014-05-31 12:43:32