gpt4 book ai didi

c# - EF Core 种子数据库与预定义列表的 OneToMany 关系

转载 作者:行者123 更新时间:2023-12-05 03:52:04 25 4
gpt4 key购买 nike

我试图在我的数据库中使用定义的列表<>播种一对多关系,但我收到以下错误消息

'The seed entity for entity type 'Country' cannot be added because it has the navigation 'WineRegions' set. To seed relationships you need to add the related entity seed to 'WineRegion' and specify the foreign key values {'CountryId'}. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the involved property values.'

我使用这个页面来创建我的类(class): https://www.learnentityframeworkcore.com/relationships#one-to-many...我只能猜测我需要以某种方式手动定义键,即使它应该自动工作...

这是我的两个类(class)。 WineRegions 包含一个 Country,而 Country 包含一组 WineRegions:

public class WineRegion
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int WineRegionId { get; set; }

public string WineRegionName { get; set; }

public int CountryId { get; set; }
public Country Country { get; set; }
}

public class Country
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CountryId { get; set; }

[Required]
[MaxLength(255)]
public string CountryName { get; set; }

[Required]
[MaxLength(2)]
public string ISO3166Alpha2 { get; set; }

public ICollection<WineRegion> WineRegions { get; set; }
}

在播种期间,我不想使用像这样的硬编码对象(顺便说一句,这段代码有效......):

        modelBuilder.Entity<Country>().HasData(
new { Id = 1, Name = "France", ISO3166Alpha2 = "FR" }
);

modelBuilder.Entity<WineRegion>().HasData(
new { Id = 1, Name = "Bordeaux", CountryId = 1 }
);

正如我之前提到的,我想使用我在 JSON 解析过程中生成的列表。

这就是我所做的:

        modelBuilder.Entity<Country>().HasData(listOfCountries);  // List<Country>
modelBuilder.Entity<WineRegion>().HasData(listOfWineRegions); //List<WineRegion>

我的 JSON 看起来像这样:

  "wineregions": [
{
"id": 1,
"country": "France",
"iso2": "FR",
"region": [
{
"id": 1,
"name": "Bordeaux"
},
{
"id": 2,
"name": "Burgundy"
},
{
"id": 4,
"name": "Burgundy"
},
{
"id": 5,
"name": "Beaujolais"
},
{
"id": 6,
"name": "Champagne"
},
{
"id": 7,
"name": "Loire"
},
{
"id": 8,
"name": "Alsace"
},
{
"id": 9,
"name": "Rhône"
},
{
"id": 10,
"name": "Provence"
},
{
"id": 11,
"name": "Languedoc-Roussillon"
},
{
"id": 12,
"name": "Jura"
}
]
},
{
"id": 2,
"country": "Italy",
"iso2": "IT",
"region": [
{
"id": 1,
"name": "Piedmont"
},
{
"id": 2,
"name": "Barolo"
},
{
"id": 3,
"name": "Barbaresco"
},
{
"id": 4,
"name": "Tuscany"
},
{
"id": 5,
"name": "Veneto"
},
{
"id": 6,
"name": "Friuli-Venezia"
},
{
"id": 7,
"name": "Giulia"
},
{
"id": 8,
"name": "Abruzzo"
},
{
"id": 9,
"name": "Sicily"
},
{
"id": 10,
"name": "Lambrusco"
}
]
}
]
}


Thanks in advance

最佳答案

您的WineRegionIdConutryId 是Identity,由SQL 自动生成。

当您的数据库 key 是身份 Id = 1 工作。

您应该从 CountryIdWineRegionId 中删除标识。

[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CountryId { get; set; }
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int WineRegionId { get; set; }

另一种方式

您可以将代码更改为此。

Country country = new Country { Name = "France", ISO3166Alpha2 = "FR" };
modelBuilder.Entity<Country>().HasData(country);

modelBuilder.Entity<WineRegion>().HasData(
new { Name = "Bordeaux", Country = country }
);

在这种情况下 ConutryIdWineRegionIdSQL 自动生成以及 ConutryWineRegion 之间的关系 由这一行创建

new { Name = "Bordeaux", Country = country/*<--NOTE THIS*/ }

关于c# - EF Core 种子数据库与预定义列表的 OneToMany 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62371307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com