Example #1
0
        static void Task8()
        {
            string townName = Console.ReadLine();

            Towns town = _context.Towns
                .FirstOrDefault(t => t.Name == townName);

            var addresses = (from a in _context.Addresses
                             where a.Town.Name == townName
                             select a)
                            .ToList();

            foreach (var a in addresses)
            {
                a.TownId = null;
            }

            _context.Remove(town);
            _context.SaveChanges();
        }
Example #2
0
        public async Task <object> Delete([FromBody] object id, [FromQuery] string type)
        {
            var assembly     = _context.GetType().Assembly;
            var assemblyName = assembly.GetName().Name;
            var objectType   = assembly.GetType($"{assemblyName}.Models.{type}");
            var idProp       = objectType.GetProperty("Id");
            var idValue      = Convert.ChangeType(id, idProp.PropertyType);
            var o            = await _context.FindAsync(objectType, idValue);

            _context.Remove(o);
            await _context.SaveChangesAsync();

            return(id);
        }