none
Audit logging using DbCommandInterceptor in EntityFramework leads to exception while trying to log the result from interceptionContext.OriginalResult RRS feed

  • Question

  • Hi,

    I have written the below code for auditlogging , I am trying to log the result content .

    this leads to exception while calling DBContext -->SaveChangs() .

    Exception and the Auditlogging code is given below

    Exception

    Save failed due to concurrency save operations
      ----> System.Data.Entity.Infrastructure.DbUpdateConcurrencyException : Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.
      ----> System.Data.Entity.Core.OptimisticConcurrencyException : Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.


    Audit logging code 

    public class NMScannerDBCommandInterceptor : DbCommandInterceptor
        { 

                    

    public override void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
            {
                WriteReadAuditLog(command, interceptionContext);
            }

            private void WriteReadAuditLog<T>(DbCommand command, DbCommandInterceptionContext<T> interceptionContext)
            {

              
                if (interceptionContext.OriginalResult != null)
                {
                    var reader = interceptionContext.OriginalResult as SQLiteDataReader;

                    if (reader != null)
                    {
                        int count = reader.FieldCount;
                        string result = Environment.NewLine;
                        while (reader.Read())
                        {
                            for (int i = 0; i < count; i++)
                            {
                                result += String.Format("{0,-10} ", reader.GetValue(i).ToString());
                            }
                            result += Environment.NewLine;
                        }
                        Console.WriteLine(result);
                    }

                }
               

    Thanks

    somaraj        }

    }

    Friday, June 26, 2020 6:09 PM

All replies

  • Hi SomarajK, 
    First, have you configure the interceptor in app.config file after implemented IDbCommandInterceptor,?
    As shown below:

    <entityFramework>
        <interceptors>
            <interceptor type="EF6DBFirstTutorials.EFCommandInterceptor, EF6DBFirstTutorials">
            </interceptor>
        </interceptors>
    </entityFramework>

    Based on  your description about exception, the error is caused by the concurrency.
    What is your code about executing a command or query before calling context.SaveChanges()?
    The general approach to handle a concurrency conflicts is:
    1.Catch DbUpdateConcurrencyException during SaveChanges.
    2.Use DbUpdateConcurrencyException.Entries to prepare a new set of changes for the affected entities.
    3.Refresh the original values of the concurrency token to reflect the current values in the database.
    More details you can refer to these documents.
    [Handling Concurrency Conflicts]
    [Handling Concurrency Conflicts]
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 29, 2020 6:15 AM