C# 在迁移中使用外键设定数据种子
我已经被提供了时区数据,我需要导入我的数据库 我所做的是读取我的数据,其格式如下:C# 在迁移中使用外键设定数据种子,c#,entity-framework,entity-framework-core,C#,Entity Framework,Entity Framework Core,我已经被提供了时区数据,我需要导入我的数据库 我所做的是读取我的数据,其格式如下: 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.Ad
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");
及
我正在做的是浏览列表,并添加所有国家
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,
});
}
}
var timezoneData=data.Timezone.getData();
//var countries=新列表();
列表存在=新列表();
int countryId=1;
foreach(时区数据中的var项)
{
var splitup=item.Split(“|”);
if(exists.Any(x=>x.Name==splitup[1]))
{
var c=新国家/地区
{
代码=拆分[0],
Description=拆分[1],
Id=countryId
};
modelBuilder.Entity().HasData(新国家[]{c});
countryId++;
存在。添加(新的现有对象)
{
名称=拆分[1],
Id=countryId,
});
}
}
然后,我需要添加所有时区。但是,因为时区有一个国家(Id),我很难在modelBuilder中处理这个外键关系。我希望我可以像这样分配FK值(注意,为了简单起见,我现在是硬编码CountryID 10)
将Country对象分配给timezone.Country会很好,但它似乎还不可用。因此,我试图提供分配给Country对象的CountryID值。(注意,现在硬编码为10)
foreach(时区数据中的变量项)
{
int timezoneId=1;
var splitup=item.Split(“|”);
modelBuilder.Entity().HasData(新时区[]{new时区
{
Id=时区Id,
CountryId=10,
Description=拆分[2],
GMTOffset=1
} });
}
但是,当我创建迁移时,会产生以下错误:
无法添加实体类型“Timezone”的种子实体,因为已添加另一个具有{Id'}相同键值的种子实体。考虑使用“dBraveTopStudioBuffel.EnabelSythViDeAtCug”来查看冲突的键值。
创建并执行SQL语句可能更容易,但这似乎与迁移无关。有没有办法实现我想要做的事情,让迁移负责添加我的参考数据,它有FK的复杂性?处理时区是编程中最麻烦的部分之一。一般来说,你试着留下来。“这就是疯狂。”这并不是夸张 在.NET中使用时区时,您的侧窗上有一个最完整的黑盒
谢谢克里斯托弗。除交易日期外,我的所有日期都存储为UTC。如果用户在日期选择器中选择日期(没有时间,我只处理日期),我将存储为所选日期。因为在澳大利亚,UTC+10,如果用户选择2019年11月22日,我将其存储为该日期。但是服务器是UTC的,所以如果我选择“今天的所有事务”,我将得到昨天的事务。我需要知道用户的时区,这样我才能得到用户的“今天”。不是服务器。你的观点非常正确。我想我正在关注它们。@Craig:交易日期是UTC中最不应该有的东西没有时间的约会没有意义。同一时间点可以是2个,有时甚至是3个不同的日期-这取决于你从哪个时区看它。除非您有caledar应用程序,否则不应尝试单独处理日期和时间。
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,
});
}
}
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
} });
}