0

Net core 3.1 project with EF core 3.1 first approach:

  • Database: MySql database
  • The context is registred in the startup method
  services.AddDbContext<TestDBContext>(options =>
              options.UseMySql(_configuration.GetConnectionString("TestDB"), mySqlOptions => mySqlOptions
                  .ServerVersion(new Version(8, 0, 20), ServerType.MySql)));

there is only one entity in the dbContext

 public partial class TestDBContext : DbContext
    {
        public DbSet<Patient> Patient { get; set; }

        public TestDBContext()
        {
        }

        public TestDBContext(DbContextOptions<TestDBContext> options)
            : base(options)
        {
        }
    }

Then in the repository layer, I add a Patient and then call .SaveChangesAsync() method, works as expected, then I modify a field, and ef is updating that field in the DB without a call .SaveChangesAsync(), why is updating without a call .SaveChangesAsync() ?

public class PatientRepository : IPatientRepository {
        private readonly TestContext _dbContext;

        public PatientRepository (TestDBContext dbContext) => _dbContext = dbContext ??
            throw new ArgumentNullException (nameof (dbContext));

        /// <inheritdoc />
        public async Task<Guid> CreatePatientAsync (Patient patient) {
            _dbContext.Patient.Add(patient);
            await _dbContext.SaveChangesAsync();

            if(string.IsNullOrEmpty( patient.PatientId))
            {
                //This part is updated without savechanges, why ?
                patient.PatientId = patient.PatientCode.ToString("D7");
            }

            return patient.Id;
        }
    }
Marztres
  • 460
  • 8
  • 15
  • 1
    Probably because your entity is being tracked and you call SaveChanges or SaveChangeAsync on the same `TestDBContext` instance later in the call stack *after* you called `CreatePatientAsync`. Another reason might be is that you incorrectly configured the `TestDBContext` type with the DI framework and registered it as a single instance. That could result in problems if you are service multiple clients or requests like in asp.net – Igor May 12 '20 at 20:04
  • 1
    One more thing I thought of. How are you calling `CreatePatientAsync`? Are you awaiting that result? – Igor May 12 '20 at 20:24
  • Yes, I am awaiting that result. I gonna check the register and if any other operation is calling the saveChangesAsync after and probably using the same instance. – Marztres May 12 '20 at 20:39
  • The current register of the context: services.AddDbContext, based on the description is being injected as a scoped object – Marztres May 12 '20 at 21:01
  • EF does not, ever save changes without explicitly call SaveChanges() or SaveChangesAsync() method. If the field you are changing is the patientId as you show in the snippet, then the problem is that you probably do not call the CreatePatientAsync methed using 'await'. So it is some sort of "racing condition" when ready to save to database what is the current state of the context? Is patientId changed yet or not? If that is the field you refer to I do not think it has to do anything with DI lifetime of your context object – Stelios Giakoumidis May 12 '20 at 21:49
  • @SteliosGiakoumidis, patient.PatientCode is an autogenerated number and is modifiying the patient.patientId after the saving. – Marztres May 13 '20 at 14:28

0 Answers0