Пример #1
0
        private void SeedData(IWebHostEnvironment env)
        {
            if (env == null)
            {
                return;
            }
            var path = $@"{env.ContentRootPath}\..\Data.yaml";

            if (!dbContext.Bridges.Any())
            {
                var deserializer = new DeserializerBuilder()
                                   .WithNamingConvention(new YamlDotNet.Serialization.NamingConventions.PascalCaseNamingConvention())
                                   .Build();

                var yamlText    = File.OpenText(path);
                var yamlObjects = deserializer.Deserialize <IEnumerable <Db.Bridge> >(yamlText);
                dbContext.Bridges.AddRange(yamlObjects);
                foreach (var bridge in dbContext.Bridges)
                {
                    foreach (var hiker in bridge.Hikers)
                    {
                        hiker.Bridge   = bridge;
                        hiker.BridgeId = bridge.Id;
                        dbContext.Hikers.Add(hiker);
                    }
                }
                dbContext.SaveChanges();
            }
        }
Пример #2
0
        private void GenerateDbContext(GeojamDbContext dbContext, int[] includeBridgeIds = null)
        {
            if (includeBridgeIds == null || !includeBridgeIds.Any())
            {
                includeBridgeIds = bridges.Select(b => b.Id).ToArray();
            }

            foreach (var bridge in bridges.Where(b => includeBridgeIds.Contains(b.Id)))
            {
                dbContext.Bridges.Add(bridge);
            }
            foreach (var hiker in hikers.Where(h => includeBridgeIds.Contains(h.BridgeId)))
            {
                dbContext.Hikers.Add(hiker);
            }

            foreach (var bridge in dbContext.Bridges)
            {
                bridge.Hikers = new List <Db.Hiker>();
                bridge.Hikers = dbContext.Hikers.Where(h => h.BridgeId == bridge.Id);
            }
            dbContext.SaveChanges();
        }