ASP.NET 核心 EF 标识 - 尝试将城市和国家/地区数据播种到单独的表中,设置 PK 不起作用
ASP.NET Core EF Identity - Trying to seed city and country data into separate tables, setting PK doesn't work
我正在尝试将国家和城市名称的初始种子放入我的数据库中,该数据库在表之间具有一对多关系。设置 PK(例如 CityId = 1)不会将 post 添加到数据库。但是,将 PK 排除在外会将 post 添加到数据库中,但如预期的那样为其提供递增的 Id,但会破坏国家/地区的关系耦合。 MS 说由于迁移更新等原因,您应该始终包含 Id,但我在这里缺少什么?搜索了几天,只得到与 MS 相同的信息。
public static void Initialize(AppIdentityDbContext context)
{
context.Database.Migrate(); ;
// Add sample countries
var countries = new Country[]
{
//Works
new Country{CountryName="Sweden"},
//Doesn't work
new Country{CountryID = 1, CountryName="Sweden"},
};
foreach (Country s in countries)
{
context.Countries.Add(s);
}
context.SaveChanges();
我通过从种子中删除 CountryID 和 CityID 解决了这个问题,因为它是在新创建的数据库的初始化过程中完成的,所以自动递增的 Id 工作正常。
我正在尝试将国家和城市名称的初始种子放入我的数据库中,该数据库在表之间具有一对多关系。设置 PK(例如 CityId = 1)不会将 post 添加到数据库。但是,将 PK 排除在外会将 post 添加到数据库中,但如预期的那样为其提供递增的 Id,但会破坏国家/地区的关系耦合。 MS 说由于迁移更新等原因,您应该始终包含 Id,但我在这里缺少什么?搜索了几天,只得到与 MS 相同的信息。
public static void Initialize(AppIdentityDbContext context)
{
context.Database.Migrate(); ;
// Add sample countries
var countries = new Country[]
{
//Works
new Country{CountryName="Sweden"},
//Doesn't work
new Country{CountryID = 1, CountryName="Sweden"},
};
foreach (Country s in countries)
{
context.Countries.Add(s);
}
context.SaveChanges();
我通过从种子中删除 CountryID 和 CityID 解决了这个问题,因为它是在新创建的数据库的初始化过程中完成的,所以自动递增的 Id 工作正常。