示例#1
0
        public void LoadFromJson()
        {
            var dirInfo = new System.IO.DirectoryInfo(@"../../../datasource/data.json");

            try
            {
                var fileContent = File.ReadAllText(dirInfo.FullName);
                var fileData    = JsonSerializer.Deserialize <FileData>(fileContent);

                Skill.AddRange(fileData.skills.Select(SkillData.ToEntity));
                SaveChanges();

                foreach (var person in fileData.persons)
                {
                    var entity = PersonData.ToEntity(person);
                    entity.Skills = Skill.Where(s => person.skills.Contains(s.Id.ToString())).ToList();
                    if (int.TryParse(person.favSkill, out int parsedValue))
                    {
                        entity.FavSkill = Skill.Find(parsedValue);
                    }
                    Person.Add(entity);
                }

                SaveChanges();

                foreach (var person in fileData.persons)
                {
                    var entity = Person.Find(int.Parse(person.id));
                    entity.Friends = Person.Where(s => person.friends.Contains(s.Id.ToString())).ToList();
                }

                SaveChanges();
            }
            catch (System.Exception)
            {
            }
        }