示例#1
0
        private int GetOrCreateCity(string name)
        {
            var entity = _context.Cities.FirstOrDefault(c => c.Name == name) ?? new City {
                Name = name
            };

            if (entity.Id == 0)
            {
                _context.Cities.Add(entity);
                _context.SaveChanges();
            }

            return(entity.Id);
        }
示例#2
0
        public (Response response, int superheroId) Create(SuperheroCreateDTO superhero)
        {
            var entity = new Superhero
            {
                Name            = superhero.Name,
                AlterEgo        = superhero.AlterEgo,
                City            = ReadOrCreateCity(superhero.CityName),
                FirstAppearance = superhero.FirstAppearance,
                Gender          = superhero.Gender,
                Occupation      = superhero.Occupation
            };

            _context.Superheroes.Add(entity);
            _context.SaveChanges();

            return(Created, entity.Id);
        }