无法用实体框架解决运行时异常 [英] Can't solve run-time exception with entity framework

查看:128
本文介绍了无法用实体框架解决运行时异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello everyone,

I am working on a project that uses EF to do crud operations against the database I'm working with. I created several stored procedures to do the insert, update and delete operations because I am doing two things, I'm adding a record to an audit table and i'm performing the crud operation itself.

My problem is that during execution of the code I get a run time exception that I haven't found an answer for online.  The Error occurs inside the Context class generated by EF.

<pre>return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<DeleteEDeliveryScheduledEvent_Result>("DeleteEDeliveryScheduledEvent", eventIdParameter, modifiedByParameter);





错误如下:



The Error is as follows:

Quote:

System.Data.Entity .Core.EntityCommandExecutionException

HResult = 0x8013193C

消息=数据读取器与指定的'AgentPortalModel.DeleteEDeliveryScheduledEvent_Result'不兼容。该类型的成员'ErrorNumber'在数据读取器中没有相应的列具有相同的名称。

System.Data.Entity.Core.EntityCommandExecutionException
HResult=0x8013193C
Message=The data reader is incompatible with the specified 'AgentPortalModel.DeleteEDeliveryScheduledEvent_Result'. A member of the type, 'ErrorNumber', does not have a corresponding column in the data reader with the same name.





我的标准删除存储过程具有以下结构:





my standard delete stored procedure has the following structure:

CREATE PROCEDURE [dbo].[DeleteEDeliveryScheduledEvent]
           @EventId INT = NULL,
           @ModifiedBy int
            
        AS 
           
       SET NOCOUNT ON; 
                
        IF @EventId IS NOT NULL

     BEGIN TRANSACTION
     BEGIN TRY
                
    INSERT INTO EDeliveryAudit
           (TableName, 
           ActionId, 
           RowXml, 
           ModifiedBy, 
           ModifiedDate)
    VALUES ('EDeliveryScheduledEvents',
           3, 
           (SELECT EventId,
                   EventDateTime
              FROM EDeliveryScheduledEvents
             WHERE EventId = @EventId
               FOR XML RAW ('ScheduledEvent'), ROOT ('ScheduledEvents'), ELEMENTS ), 
           @ModifiedBy, 
           GETDATE())

    DELETE FROM EDeliveryScheduledEvents
     WHERE EventId = @EventId
            
       END TRY
     BEGIN CATCH
           
    SELECT ERROR_NUMBER() AS ErrorNumber,
           ERROR_SEVERITY() AS ErrorSeverity,
           ERROR_STATE() AS ErrorState,
           ERROR_PROCEDURE() AS ErrorProcedure,
           ERROR_LINE() AS ErrorLine,
           ERROR_MESSAGE() AS ErrorMessage;
           
        IF @@TRANCOUNT > 0
  ROLLBACK TRANSACTION       
       
       END CATCH
           
        IF @@TRANCOUNT > 0
    COMMIT TRANSACTION





我尝试过:



我试过使用参数。

我尝试使用ObjectContext直接调用EF函数



What I have tried:

I've tried using out parameters.
I've tried using the ObjectContext to invoke the EF function directly

推荐答案

解决了它!



我不能说我理解为什么,但实体框架在catch中看到了Select语句并且正在配置复杂类型期望这些列作为输出的一部分(ERROR_NUMBER,ERROR_SEVERITY,ETC。)。存储过程正在运行而没有错误,并返回一个新的标识或指示成功或失败的位,EF正在抱怨缺少的列。



我删除了select语句从所有存储过程中删除我已编码到sp中的任何返回值。 (有些表没有标识字段,因此我返回1或0)。



我不确定这将如何影响Catch中的Catch语句存储过程,但代码现在运行而不会崩溃。
Solved it!

I can't say I understand why, but Entity Framework was seeing the Select statement inside the catch and was configuring the complex types to expect those columns as part of the output (ERROR_NUMBER, ERROR_SEVERITY, ETC.). The stored procedures were running without error and returning either a new Identity or a bit indicating success or failure and EF was complaining about the missing columns.

I removed the select statement from all the stored procedures and also removed any return values I had coded into the sp. (Some tables don't have an identity field and so I was returning 1 or 0).

I'm not sure how this will affect the Catch Statement in the stored procedure, but the code now runs without crashing.


这篇关于无法用实体框架解决运行时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆