Когда я запускаю следующий код, я получаю исключение, в котором говорится, что «Тип объекта ElementWeight требует определения первичного ключа». Зачем мне нужен ключ, если я хочу рассматривать ElementWeight как тип значения?
var context = new DataContext(options);
context.Weightings.Add(new ImportWeighting { Name = "temp", Weights = new List<ElementWeight>
{ new ElementWeight { Mag_dB = 1.0, Phase_deg = 10.0 },
new ElementWeight { Mag_dB = 2.0, Phase_deg = 20.0 }
} });
В OnModelCreating я делаю следующее
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ImportWeighting>().OwnsMany(c => c.Weights);
}
А вот и занятия
public class ImportWeighting
{
public int Id { get; set; }
public string Name { get; set; }
public List<ElementWeight> Weights { get; set; }
}
public class ElementWeight
{
public double Mag_dB { get; set; }
public double Phase_deg { get; set; }
}
Если я добавлю ключ, как показано в документации, я получаю сообщение об ошибке, что элементы уже существуют.
modelBuilder.Entity<Distributor>().OwnsMany(p => p.ShippingCenters, a =>
{
a.HasForeignKey("DistributorId");
a.Property<int>("Id");
a.HasKey("DistributorId", "Id");
});