繁体   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