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, List1& parameters, Boolean1& parameters, CommandType&
isLocalProvider)
… System.Data.SqlServerCe.SqlGen.SqlGenerator.GenerateSql(DbCommandTree
tree, List
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, Dictionary22
identifierValues)
… System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator
translator, EntityConnection
connection, Dictionary
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
idexplicitly. Here is example.You have to maintain
lastId. Code that I wrote above will work only in single thread.