none
Transaction EF6 RRS feed

  • Question

  • I am using SQLite database. I am not disposing of DB context when the transaction is rolled back. After the transaction is rolled back. And inserting again the same type of entities which is inserted in the transaction (that was rolled back) it throws an exception mentioned below. Steps to reproduce the error :

    STEP 1. Transaction starts :

    _transaction = _dbContext.Database.BeginTransaction();

    STEP 2. Insert Records :

    _dbContext.EntityName.Add(newRecords);

    STEP 3. Save Changes :

    _dbContext.SaveChanges();

    STEP 4: Roll Back Transaction:

    _transaction.Rollback();
    _transaction.Dispose();
    _transaction = null;

    STEP 5: Repeat STEP 1,2,3. STEP 3 throws Exception above mentioned

    Note - _dbContext is not disposed.

    InvalidOperationException: Saving or accepting changes failed because more than one entity of type 'EntityName' has the same primary key value. Ensure that explicitly set primary key values are unique. Ensure that database-generated primary keys are configured correctly in the database and in the Entity Framework model. Use the Entity Designer for Database First/Model First configuration. Use the 'HasDatabaseGeneratedOption" fluent API or 'DatabaseGeneratedAttribute' for Code First configuration.

    Friday, July 19, 2019 6:11 AM