Я строю проект, используя DDD. Таким образом, единственный способ обновить / добавить / удалить дочернюю сущность - через ее родителя, это не было проблемой, когда я использовал do tnet core 2.0, но теперь, когда я перевожу проект на tnet core 3.1 I получаю следующую ошибку:
Предполагается, что операция с базой данных затронет 1 строку (и), но фактически затронет 0 строк. Данные могут быть изменены или удалены с момента загрузки объектов. См. http://go.microsoft.com/fwlink/?LinkId=527962 для получения информации о понимании и обработке оптимистических c исключений параллелизма.
У меня есть такой класс клиента:
public class Client
{
/// <summary>
/// DDD Patterns comment Using a private collection field, better for DDD Aggregate's
/// encapsulation so Receptions cannot be added from "outside the AggregateRoot" directly to
/// the collection, but only through the method ClientAggrergateRoot.AddReception() which
/// includes behaviour.
/// </summary>
private readonly List<Reception> _receptions;
public Client(Guid id, string name, string url, string domainEmail)
{
Id = id;
Name = name;
Url = url;
DomainEmail = domainEmail;
_receptions = new List<Reception>();
}
protected Client()
{
_receptions = new List<Reception>();
}
public string Name { get; private set; }
public string Url { get; private set; }
public string DomainEmail { get; private set; }
public bool IsDeleted { get; private set; }
public DateTime CreatedAt { get; private set; }
public DateTime ModifiedAt { get; private set; }
public IReadOnlyCollection<Reception> Receptions => _receptions;
}
I есть мой класс приема:
public class Reception
{
public Reception(Guid id, string name, string address, string noteToGuest, string noteToReceptionst,
string timeZone, Guid clientId)
{
Id = id;
Name = name;
Address = address;
ClientId = clientId;
}
private Reception()
{
}
public Guid ClientId { get; private set; }
public string Name { get; private set; }
public string Address { get; private set; }
}
И это файл конфигурации клиента:
public class ClientEntityTypeConfiguration
: IEntityTypeConfiguration<Client>
{
public void Configure(EntityTypeBuilder<Client> builder)
{
builder.ToTable("Clients", BeWelcomeContext.CLIENT_SCHEMA);
builder.HasKey(c => c.Id);
var navigation = builder.Metadata.FindNavigation(nameof(Client.Receptions));
// DDD Patterns comment: Set as field (New since EF 1.1) to acces the Receptions
// collection property through its fields
navigation.SetPropertyAccessMode(PropertyAccessMode.Field);
}
}
И файл конфигурации приема:
public class ReceptionEntityTypeConfiguration
: IEntityTypeConfiguration<Reception>
{
public void Configure(EntityTypeBuilder<Reception> builder)
{
builder.ToTable("Receptions", BeWelcomeContext.CLIENT_SCHEMA);
builder.HasKey(r => r.Id);
builder.HasOne<Client>()
.WithMany(r => r.Receptions)
.IsRequired()
.HasForeignKey(r => r.ClientId);
}
}
Это сработало отлично пока я не перенесу проект, я не знаю, что происходит, если мне придется изменить какую-либо конфигурацию.