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;
}
}