簡體   English   中英

將SQL Server Compact與實體框架一起使用時的UpdateException

[英]UpdateException when using SQL Server Compact with Entity Framework

我正在嘗試將實體框架與SQL Server Compact結合使用。 我可以執行以下閱讀:

var context = new TestEntities();

foreach (var p in context.Personnes)
{
    Console.WriteLine(p.prenom + " " + p.nom);
}

但我不能執行插入:

var context = new TestEntities();

context.Personnes.AddObject(new Personne() {nom = "Test", prenom = "Test" });

context.SaveChanges();

當我嘗試執行此操作時,將引發UpdateException Personne表只有3列: id (int, primary key), nom (varchar) and prenom (varchar).

這是我運行程序時顯示的內容:

System.Data.EntityCommandCompilationException:完全無法正常執行命令的定義。 倒加細末,咨詢例外情況。 ---> System.NotSupportedException:SQL Server Compact附帶的注釋和用法。

System.Data.SqlServerCe.SqlGen.DmlSqlGenerator.GenerateInsertSql(DbInsertCommandTree樹,列表1& parameters, Boolean isLocalProvider) … System.Data.SqlServerCe.SqlGen.SqlGenerator.GenerateSql(DbCommandTree tree, List 1&參數,CommandType&commandType,布爾值isLocalProvider)…系統... .Common.DbProviderServices.CreateCommand(DbCommandTree commandTree)…System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)---例外情況跟蹤---…System.Data。 Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree命令 樹)…System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(UpdateTranslator轉換器,字典2 identifierValues) … System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary 2標識符值,列表`1 generateValues)…System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager,IEntityAdapter適配器)

謝謝 :)

SqlServer Compact不支持服務器端密鑰生成。 您必須在應用程序端生成密鑰。 因此,明確設置id 這是例子。

context.Personnes.AddObject(new Personne() {id = lastId++, nom = "Test", prenom = "Test" });

您必須維護lastId 我上面編寫的代碼僅在單線程中有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM