Я новичок и в DDD, и в Entity Framework Core, и я пытаюсь заполнить запись сущности в моей первой и единственной таблице базы данных. К сожалению, я получаю эту ошибку при попытке добавить миграцию ....
The seed entity for entity type 'SnackMachine' with the key value 'Id:daacbca0-f425-49d9-9cdb-
feb7ece42188' cannot be added because it has the navigation 'MoneyInside' set. To seed relationships
you need to add the related entity seed to 'Money' and specify the foreign key values {'SnackMachineId'}.
Вот мой класс сущности ...
public class SnackMachine : Entity
{
public Money MoneyInside { get; private set; } = Money.None;
public Money MoneyInTransaction { get; private set; } = Money.None;
public SnackMachine()
{
MoneyInside = Money.None;
MoneyInTransaction = Money.None;
}
public void InsertMoney(Money money)
{
// Removed for brevity
}
// Rest of domain logic removed for brevity
}
А вот мой Money
объект значения ...
public class Money : ValueObject<Money>
{
public int OneCentCount { get; }
public int TenCentCount { get; }
public int QuarterCount { get; }
public int OneDollarCount { get; }
public int FiveDollarCount { get; }
public int TwentyDollarCount { get; }
private Money() { }
public Money(int oneCentCount, int tenCentCount, int quarterCentCount, int oneDollarCount,
int fiveDollarCount, int twentyDollarCount)
{
// Checks removed for brevity
OneCentCount = oneCentCount;
TenCentCount = tenCentCount;
QuarterCount = quarterCentCount;
OneDollarCount = oneDollarCount;
FiveDollarCount = fiveDollarCount;
TwentyDollarCount = twentyDollarCount;
}
// Equality checks removed for brevity
}
Мне удалось создать структуру базы данных так, как я хотел бы ...
... но у меня проблемы с посевом с записью. Вот мой свободный API ...
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SnackMachine>()
.Property(s => s.Id)
.HasColumnName("Id")
.IsRequired();
modelBuilder.Entity<SnackMachine>().OwnsOne(s => s.MoneyInside, m =>
{
m.Property(p => p.OneCentCount)
.HasColumnName("OneCentCount")
.HasDefaultValue(0);
m.Property(p => p.TenCentCount)
.HasColumnName("TenCentCount")
.HasDefaultValue(0);
m.Property(p => p.QuarterCount)
.HasColumnName("QuarterCount")
.HasDefaultValue(0);
m.Property(p => p.OneDollarCount)
.HasColumnName("OneDollarCount")
.HasDefaultValue(0);
m.Property(p => p.FiveDollarCount)
.HasColumnName("FiveDollarCount")
.HasDefaultValue(0);
m.Property(p => p.TwentyDollarCount)
.HasColumnName("TwentyDollarCount")
.HasDefaultValue(0);
});
modelBuilder.Entity<SnackMachine>().Ignore(s => s.MoneyInTransaction);
var snackMachine = new SnackMachine
{
Id = new Guid("daacbca0-f425-49d9-9cdb-feb7ece42188"),
};
snackMachine.InsertMoney(new Money(1, 0, 0, 0, 0, 0));
snackMachine.InsertMoney(new Money(0, 1, 0, 0, 0, 0));
snackMachine.InsertMoney(new Money(0, 0, 1, 0, 0, 0));
snackMachine.InsertMoney(new Money(0, 0, 0, 1, 0, 0));
snackMachine.InsertMoney(new Money(0, 0, 0, 0, 1, 0));
snackMachine.InsertMoney(new Money(0, 0, 0, 0, 0, 1));
modelBuilder.Entity<SnackMachine>().HasData(snackMachine);
}
Мне не нужен внешний ключ от Money
до SnackMachine
, поскольку он нарушает идею объекта-значения. Что я тут не так делаю?
Спасибо