для простоты, рассмотрите следующий код: < /p>
Код: Выделить всё
public class Class1 {
public int? Property1 {get;set;}
public int? Property2 {get;set;}
}
// Context
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity(entity =>
{
entity.Property(e => Property1).HasDefaultValue(1);
entity.Property(e => Property2).HasDefaultValue(1);
});
}
// --
// lets assume the entry in the DB already exists and Property2 has value 0.
var InstanceOfClass1 = await GetInstanceOfClassFromDB();
InstanceOfClass1.Property1++;
InstanceOfClass1.Property2++;
Console.WriteLine(InstanceOfClass1.Property1); // 1
Console.WriteLine(InstanceOfClass1.Property2); // 1
using var context = await _Factory.CreateDbContextFactoryAsync();
context.Classes.Update(InstanceOfClass1);
await context.SaveChangesAsync();
Console.WriteLine(InstanceOfClass1.Property1); // 1
Console.WriteLine(InstanceOfClass1.Property2); // 0
Подробнее здесь: https://stackoverflow.com/questions/796 ... by-ef-core