예제 #1
0
        public U?GetId(string name)
        {
            if (name == null)
            {
                return(null);
            }

            if (!NameMap.ContainsKey(name))
            {
                using (Models.BodyDbContext ctx = new Models.BodyDbContext())
                {
                    T map = ctx.Set <T>().FirstOrDefault(v => v.Name == name);

                    if (map == null)
                    {
                        map = new T {
                            Name = name
                        };
                        ctx.Set <T>().Add(map);
                        ctx.SaveChanges();
                    }

                    IdMap[map.Id]     = map;
                    NameMap[map.Name] = map;
                }
            }

            return(NameMap[name].Id);
        }
예제 #2
0
        public NameIdMap(IEnumerable <T> seed)
        {
            using (Models.BodyDbContext ctx = new Models.BodyDbContext())
            {
                T[] maps = ctx.Set <T>().ToArray();

                if (maps.Length == 0)
                {
                    maps = seed.ToArray();
                    ctx.Set <T>().AddRange(maps);
                    ctx.SaveChanges();
                }

                foreach (T map in maps)
                {
                    NameMap[map.Name] = map;
                    IdMap[map.Id]     = map;
                }
            }
        }