Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
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


What I have tried:

I've tried using out parameters.
I've tried using the ObjectContext to invoke the EF function directly
Posted
Updated 10-Aug-18 9:16am
Comments
Bryian Tan 10-Aug-18 12:31pm    
the error is complaining "ErrorNumber" is not a property of DeleteEDeliveryScheduledEvent_Result object / table / entity. Did you verify that?
Member 11331145 10-Aug-18 14:22pm    
ErrorNumber is not a part of my table and it isn't supposed to be part of the output. the only part where I have ErrorNumber is inside the stored procedure in the catch clause where I am rolling back the transaction if an error occurs.

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900