简体   繁体   English

处理EntityFramework存储过程映射上的返回值

[英]Handling return values on EntityFramework stored procedure mapping

Using the Entity Framework stored procedure mapping I am inserting records into a table. 使用实体框架存储过程映射,我正在将记录插入表中。 Before the insert I will do some validation based on that I will insert the record and return the id. 在插入之前,我将基于此进行一些验证,然后插入记录并返回ID。

When validation fails I will return 0 because of that EF throws the following error: 验证失败时,由于EF引发以下错误,我将返回0:

The changes to the database were committed successfully, but an error occurred while updating the object context. 已成功提交对数据库的更改,但是在更新对象上下文时发生错误。 The ObjectContext might be in an inconsistent state. ObjectContext可能处于不一致状态。
Inner exception message: AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. 内部异常消息:AcceptChanges无法继续,因为对象的键值与ObjectStateManager中的另一个对象冲突。 Make sure that the key values are unique before calling AcceptChanges. 在调用AcceptChanges之前,请确保键值是唯一的。

Stored procedure code: 存储过程代码:

alter PROCEDURE [dbo].[testpro]
   @field1 VARCHAR(20),
   @field2  varchar(20)
AS
   DECLARE @field3 INT;
   DECLARE @id INT=0; 

   BEGIN TRANSACTION
   SAVE TRANSACTION STARTWORK

      SELECT @field3 = ID 
      FROM anothertable 
      WHERE field = @field1 

      IF @field3 <>0 
      BEGIN
         --INSERT(@field,@field2,@field3)
         SET @ID = SCOPE_IDENTITY()
      END

      COMMIT TRANSACTION       

      SELECT @ID AS ID
  GO

Entity Framework code: 实体框架代码:

records.ForEach((record) =>
            {
                    repository.Add(record);
            });

repository.UnitOfWork.Save();

I know the reason for error, but how to handle this issue ? 我知道错误的原因,但是如何处理此问题?

I am using EF 5 我正在使用EF 5

Note: Above code works if IF condition get passed, it throws error only when I'd value is not populated 注意:上面的代码在IF条件通过的情况下有效,仅当未填充值时才会引发错误

This usually happens when primary keys of context don't match with the database. 当上下文的主键与数据库不匹配时,通常会发生这种情况。 You can update your model and try again. 您可以更新模型,然后重试。

You must map the ID returned by your stored procedure to the ID of your entity in Entity Framework. 您必须将存储过程返回的ID映射到Entity Framework中实体的ID。 You do so by adding a result column binding in the stored procedure mapping window. 为此,您可以在存储过程映射窗口中添加结果列绑定。

Otherwise, the ObjectContext does not know the ID for the object has changed, and will keep it as 0. When EF then tries to insert the second record it succeeds, but it finds itself with two records with the same primary key (0). 否则, ObjectContext不知道对象的ID已更改,并将其保持为0。当EF然后尝试插入第二条记录时,它会成功,但是会发现自己具有两条具有相同主键(0)的记录。

This is why it says The changes to the database were committed successfully, but an error occurred while updating the object context. 这就是为什么它说The changes to the database were committed successfully, but an error occurred while updating the object context.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM