2011-01-19 60 views
0

我正嘗試使用實體框架與SQL Server Compact。我可以進行閱讀,如:使用SQL Server Compact與實體框架時的UpdateException

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: 的Une ERREUR s'est produite的LOR德拉 準備去ladéfinitionde la commande。傾注加分, consultez l'exception interne。 ---> System.NotSupportedException:Lesclès et les valeursgénéréespar le serveur ne sont pas prises en charge par SQL Server Compact。

System.Data.SqlServerCe.SqlGen.DmlSqlGenerator.GenerateInsertSql(DbInsertCommandTree 樹,列表參數的CommandType & 命令類型,布爾isLocalProvider) ... System.Data.SqlServerCe.SqlCeProviderServices.CreateCommand(DbProviderManifest providerManifest, DbCommandTree commandTree) ... System.Data.SqlServerCe.SqlCeProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest,DbCommandTree commandTree) ... System.Data.Common.DbProviderServices.CreateCommandDe FINITION(DbCommandTree commandTree) ... System.Data.Common.DbProviderServices.CreateCommand(DbCommandTree commandTree) ... System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree) ---翅德拉跟蹤德拉樁D'例外實習醫生--- ... System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree) ... System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(UpdateTranslator 翻譯, Dictionary identifierValues,List`1 generatedValues) ... System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager,IEntityAdapter適配器)

謝謝:)

+3

那麼這是一場猜測錯誤是什麼的比賽,還是您想與我們分享,也許會得到答案? – 2011-01-19 14:11:53

+0

英文是這裏的主要語言,請將錯誤文本翻譯爲英文。 – Andrey 2011-01-19 14:19:21

回答

1

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

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

您必須維護lastId。我上面寫的代碼只能在單線程中使用。

相關問題