使用实体框架SQL Server Compact时的UpdateException [英] UpdateException when using SQL Server Compact with Entity Framework
问题描述
var context = new TestEntities();
foreach(var p in context.Personnes)
{
Console.WriteLine(p.prenom ++ p.nom);
}
但我无法执行insert:
var context = new TestEntities();
context.Personnes.AddObject(new Personne(){nom =Test,prenom =Test});
context.SaveChanges();
当我尝试使用 UpdateException
做这个。 Personne
表只有3列: id(int,primary key),nom(varchar)和prenom(varchar)。
以下是运行程序时的显示:
System.Data.EntityCommandCompilationException:
Une erreur s'est produite lors de la
préparationde ladéfinitionde la
commande。 Pour plus dedétails,
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
tree,List1& parameters,Boolean
1& parameters,CommandType&
isLocalProvider)
... System.Data.SqlServerCe.SqlGen.SqlGenerator.GenerateSql(DbCommandTree
tree,List
commandType,Boolean isLocalProvider)
...系统。 Data.SqlServerCe.SqlCeProviderServices.CreateCommand(DbProviderManifest
providerManifest,DbCommandTree
commandTree)
... System.Data.SqlServerCe.SqlCeProviderServices.CreateDbCommandDefinition(DbProviderManifest
providerManifest,DbCommandTree
commandTree )
... System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree
commandTree)
... Sy (DbCommandTree
commandTree)
... System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree
commandTree)
--- Fin de la trace de la pile d'exception interne ---
... System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree
commandTree)
... System.Data.Mapping更新的内容
翻译器,EntityConnection
连接,字典 2
identifierValues,List`1
generatedValues)
... 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
。我上面写的代码只能在单线程中使用。
I am trying to use Entity Framework with SQL Server Compact. I can perform reading such as :
var context = new TestEntities();
foreach (var p in context.Personnes)
{
Console.WriteLine(p.prenom + " " + p.nom);
}
but I can't perform insert :
var context = new TestEntities();
context.Personnes.AddObject(new Personne() {nom = "Test", prenom = "Test" });
context.SaveChanges();
An UpdateException
is raised when I try to do this. The Personne
table has just 3 columns: id (int, primary key), nom (varchar) and prenom (varchar).
Here is the display I have when I run the program :
System.Data.EntityCommandCompilationException: Une erreur s'est produite lors de la préparation de la définition de la commande. Pour plus de détails, consultez l'exception interne. ---> System.NotSupportedException: Les clès et les valeurs générées par le serveur ne sont pas prises en charge par SQL Server Compact.
System.Data.SqlServerCe.SqlGen.DmlSqlGenerator.GenerateInsertSql(DbInsertCommandTree tree, List
1& parameters, Boolean isLocalProvider) … System.Data.SqlServerCe.SqlGen.SqlGenerator.GenerateSql(DbCommandTree tree, List
1& parameters, CommandType& commandType, Boolean isLocalProvider) … System.Data.SqlServerCe.SqlCeProviderServices.CreateCommand(DbProviderManifest providerManifest, DbCommandTree commandTree) … System.Data.SqlServerCe.SqlCeProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree) … System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree) … System.Data.Common.DbProviderServices.CreateCommand(DbCommandTree commandTree) … System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree) --- Fin de la trace de la pile d'exception interne --- … System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree) … System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(UpdateTranslator translator, Dictionary2 identifierValues) … System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary
2 identifierValues, List`1 generatedValues) … System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
Thank you :)
SqlServer Compact doesn't support server-side key generation. You must generate key on application side. So set id
explicitly. Here is example.
context.Personnes.AddObject(new Personne() {id = lastId++, nom = "Test", prenom = "Test" });
You have to maintain lastId
. Code that I wrote above will work only in single thread.
这篇关于使用实体框架SQL Server Compact时的UpdateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!