Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在迁移中使用外键设定数据种子_C#_Entity Framework_Entity Framework Core - Fatal编程技术网

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日期时间。在中使用本地时区通常是一个显示端问题
  • 切勿以字符串形式存储、检索或传输
  • 如果您不能遵循规则2,至少选择一种固定的编码和区域性格式。现有的序列化方法倾向于为您完成这一部分
  • ToString()能够在输出期间在中使用本地时区。它为此使用完整的窗口规则集。它将检索用户为此选择的时区
  • 如果您有日历应用程序,所有这些规则都会失败。但在这种情况下,我认为DateTime本身可能是错误的工具

  • 谢谢克里斯托弗。除交易日期外,我的所有日期都存储为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
                    } });
    
                }