예제 #1
0
        public async Task DeleteAsync(int id)
        {
            PropertyListing property = await _db.Properties
                                       .Include(p => p.Metadata)
                                       .Include(p => p.Media)
                                       .Include(p => p.FloorPlans)
                                       .Where(p => p.Id == id)
                                       .FirstOrDefaultAsync();

            property.Media.ForEach(async m =>
            {
                try { await _media.DeleteStoredMedia(m); } catch (Exception) { }
                _db.Entry(m).State = EntityState.Deleted;
            });
            property.FloorPlans.ForEach(async m =>
            {
                try { await _media.DeleteStoredMedia(m); } catch (Exception) { }
                _db.Entry(m).State = EntityState.Deleted;
            });
            property.Metadata.ForEach(m =>
            {
                _db.Entry(m).State = EntityState.Deleted;
            });
            await _db.SaveChangesAsync();

            _db.Entry(property).State = EntityState.Deleted;
            await _db.SaveChangesAsync();

            ClearPropertyCache(id);
        }