【发布时间】:2019-11-22 05:26:14
【问题描述】:
我已获得需要导入数据库的时区数据。
我所做的是读取我的数据,格式为:
List<string> items = new List<string>();
items.Add("AF|Afghanistan|Asia/Kabul|UTC +04:30");
items.Add("AX|Aland Islands|Europe/Mariehamn|UTC +02:00");
items.Add("AL|Albania|Europe/Tirane|UTC +01:00");
items.Add("DZ|Algeria|Africa/Algiers|UTC +01:00");
items.Add("AS|American Samoa|Pacific/Pago_Pago|UTC -11:00");
items.Add("AD|Andorra|Europe/Andorra|UTC +01:00");
items.Add("AO|Angola|Africa/Luanda|UTC +01:00");
items.Add("AI|Anguilla|America/Anguilla|UTC -04:00");
items.Add("AQ|Antarctica|Antarctica/Casey|UTC +08:00");
items.Add("AQ|Antarctica|Antarctica/Davis|UTC +07:00");
items.Add("AQ|Antarctica|Antarctica/DumontDUrville|UTC +10:00");
items.Add("AQ|Antarctica|Antarctica/Mawson|UTC +05:00");
items.Add("AQ|Antarctica|Antarctica/McMurdo|UTC +13:00");
items.Add("AQ|Antarctica|Antarctica/Palmer|UTC -03:00");
所以,一个国家可以有多个时区。
我有两个实体需要进入:
[Table("Country", Schema = "ref")]
public class Country
{
[Key, Required]
public int Id { get; set; }
[Required, MaxLength(2)]
public string Code { get; set; }
[Required]
public string Description { get; set; }
}
和
public class Timezone
{
[Key, Required]
public int Id { get; set; }
[Required]
public Country Country { get; set; }
[Required]
public string Description { get; set; }
[Required]
public int GMTOffset { get; set; }
[ForeignKey("Country")]
public int CountryId { get; set; }
}
我正在做的是浏览列表,并添加所有国家/地区。
var timezoneData = data.Timezone.getData();
//var countries = new List<Country>();
List<ExistingObject> exists = new List<ExistingObject>();
int countryId = 1;
foreach (var item in timezoneData)
{
var splitup = item.Split('|');
if (exists.Any(x => x.Name == splitup[1]))
{
var c = new Country
{
Code = splitup[0],
Description = splitup[1],
Id = countryId
};
modelBuilder.Entity<Country>().HasData(new Country[] { c });
countryId++;
exists.Add(new ExistingObject
{
Name = splitup[1],
Id = countryId,
});
}
}
然后,我需要添加所有时区。但是,因为 Timezone 有一个 Country(Id),所以我很难在 modelBuilder 中处理这种外键关系。我希望我可以像这样分配 FK 值(注意,为了简单起见,我现在是硬编码 CountryID 10)
将 Country 对象分配给 timezone.Country 会很好,但它似乎还不可用。所以我试图提供我分配给 Country 对象的 CountryID 值。 (注意,现在硬编码 10)
foreach (var item in timezoneData)
{
int timezoneId = 1;
var splitup = item.Split('|');
modelBuilder.Entity<Timezone>().HasData(new Timezone[] { new Timezone
{
Id = timezoneId,
CountryId = 10,
Description = splitup[2],
GMTOffset = 1
} });
}
但是,当我创建迁移时,它会产生错误:
无法添加实体类型“时区”的种子实体,因为已经添加了另一个具有与 {'Id'} 相同键值的种子实体。考虑使用“DbContextOptionsBuilder.EnableSensitiveDataLogging”来查看冲突的键值。
创建并执行 SQL 语句可能更容易,但这似乎与迁移无关。有没有办法实现我想要做的事情,并让迁移负责添加我的参考数据,这具有 FK 的复杂性?
【问题讨论】:
标签: c# entity-framework entity-framework-core