Entity Framework Core:使用 OwnsOne 时出现种子数据错误

问题描述 投票:0回答:0

我在尝试使用 Entity Framework Core 为

Country
实体播种数据时遇到问题。

我的实体类设置如下:

public class Country
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Iso3 { get; set; }
    public Currency Currency { get; set; }
}

public class Currency
{
    public Currency(string code, string name, string symbol)
    {
        Code = code;
        Name = name;
        Symbol = symbol;
    }

    public string Code { get; set; }
    public string Name { get; set; }
    public string Symbol { get; set; }
}

在我的

DbContext
中,我使用
Country
设置了
EntityTypeConfiguration
实体的配置,包括与
Currency
实体的一对一关系:

public class CountryEntityTypeConfiguration : IEntityTypeConfiguration<Country>
{
    public void Configure(EntityTypeBuilder<Country> builder)
    {
        builder.ToTable("countries");

        builder.HasKey(e => e.Id);

        builder.Property(e => e.Name)
               .HasMaxLength(100)
               .IsRequired();
        builder.Property(e => e.Iso3)
               .HasMaxLength(3)
               .IsRequired();

        builder.OwnsOne(e => e.Currency, c => {
            c.Property(_ => _.Code)
             .HasMaxLength(3)
             .IsRequired();

            c.Property(_ => _.Name)
             .HasMaxLength(45)
             .IsRequired();

            c.Property(_ => _.Symbol)
             .HasMaxLength(10)
             .IsRequired();
        });
    }
}

我的迁移完全按照我的预期创建了表:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.CreateTable(
        name: "countries",
        columns: table => new
        {
            id = table.Column<int>(type: "integer", nullable: false)
                .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
            name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
            iso3 = table.Column<string>(type: "character varying(3)", maxLength: 3, nullable: false),
            capital = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
            currency_code = table.Column<string>(type: "character varying(3)", maxLength: 3, nullable: false),
            currency_name = table.Column<string>(type: "character varying(45)", maxLength: 45, nullable: false),
            currency_symbol = table.Column<string>(type: "character varying(10)", maxLength: 10, nullable: false),
        },
        constraints: table =>
        {
            table.PrimaryKey("pk_countries", x => x.id);
        });
}

我的种子:

modelBuilder.Entity<Country>().HasData(new Country { Id = 1, Name = "Afghanistan", Iso3 = "AFG", Currency = new Currency(code: "AFN", name: "Afghan afghani", symbol: "؋")});

但是,当我尝试使用下面的迁移为

Country
实体播种数据时,出现错误:

无法添加键值为“Id:1”的实体类型“Country”的种子实体,因为它设置了导航“Currency”。要种子关系,请将实体种子添加到“Currency”并指定外键值 {'CountryId'}。

我不确定如何解决此错误并成功使用相关

Country
数据为
Currency
实体播种数据。

如有任何帮助,我们将不胜感激。谢谢!

entity-framework entity-framework-core
© www.soinside.com 2019 - 2024. All rights reserved.