EntityFramework6针对Npgsql创建了无效的更新语句

3
我正在使用Npgsql EF6提供程序(Npgsql v3.0.5.0)。 我从现有的数据库生成了模型。 我可以访问数据库并读取记录,但更新现有记录失败。
以下是我代码的简化版本:
int jobId = 123; // some unique id
Entities entities = new Entities();
Job job = entities.Jobs.First(j => j.Id = jobId);
job.Status = 4;
entities.SaveChanges(); // this line throws an exception

这是我从这段代码得到的错误消息:

42601: syntax error at or near "("

[NpgsqlException (0x80004005): 42601: syntax error at or near "("]
   Npgsql.NpgsqlConnector.DoReadSingleMessage(DataRowLoadingMode dataRowLoadingMode, Boolean returnNullForAsyncMessage, Boolean isPrependedMessage) +445
   Npgsql.NpgsqlConnector.ReadSingleMessage(DataRowLoadingMode dataRowLoadingMode, Boolean returnNullForAsyncMessage) +282
   Npgsql.NpgsqlCommand.Execute(CommandBehavior behavior) +270
   Npgsql.NpgsqlCommand.ExecuteNonQueryInternal() +177
   Npgsql.NpgsqlCommand.ExecuteNonQuery() +21
   System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<NonQuery>b__0(DbCommand t, DbCommandInterceptionContext`1 c) +26
   System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) +129
   System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext) +602
   System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery() +151
   System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues, List`1 generatedValues) +1045
   System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() +221

这是正在对数据库运行的查询:

UPDATE (
    SELECT 
        "Jobs"."Id", 
        "Jobs"."Name", 
        "Jobs"."Created", 
        "Jobs"."Status"
    FROM "Jobs"."Jobs" 
    AS "Jobs"
) SET "Status"=$1 WHERE "Id" = $2

我理解这个查询明显不能使用嵌套的SELECT而不是表名。

是否有人遇到过相同的错误?我可能错过了某些设置吗?

1个回答

2

这个错误是由于受影响的表缺少主键引起的。

.edmx 文件实际上包含了以下信息:

warning 6002: The table/view '[database.schema.TableName]' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view.

当从数据库更新模型时,Visual Studio在输出选项卡中显示以下信息(第一次生成模型时可能会错过):

The model was generated with warnings or errors.ServiceModel.edmxPlease see the Error List for more details. These issues must be fixed before running your application.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接