Код: Выделить всё
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddDbContext();
var app = builder.Build();
var db = app.Services.GetRequiredService();
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
< /code>
У меня есть два типа объектов с отношениями с одним ко многим: < /p>
class Person
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public int CountryId { get; set; }
public Country Country { get; set; } = null!;
}
class Country
{
public int Id { get; set; }
public ICollection
People { get; set; } = [];
}
class MyDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseSqlServer("connectionString");
public DbSet People { get; set; } = null!;
public DbSet Countries { get; set; } = null!;
}
< /code>
Вот мой код для посева: < /p>
Person[] people = [
new Person { Name = "A" },
new Person { Name = "A1" },
new Person { Name = "A2" },
new Person { Name = "A3" }
];
Country[] countries = [
new Country { People = people },
new Country { People = people },
new Country { People = people },
];
db.Countries.AddRange(countries);
< /code>
Когда я называю db.countries.addrange (страны); < /code>, происходит следующее исключение: < /p>
system.notsupportedExcept что массив People Подробнее здесь: https://stackoverflow.com/questions/796 ... on-when-us