public IEnumerable <string> GetCountryList() { using (var context = new BaseInfoDbContext(_connectionString)) { return(context.Countries.Select(c => c.CountryName).ToList()); } }
public void DeleteHolder(Guid holderId) { using (var context = new BaseInfoDbContext(_connectionString)) { var holder = context.Find <Holder>(holderId); DeleteHolder(holder.Moniker); } }
public void UpdateHolder(Guid holderId, Holder holderData) { using (var context = new BaseInfoDbContext(_connectionString)) { context.Holders.Update(holderData); context.Entry(holderData).State = EntityState.Modified; context.SaveChanges(); } }
public Holder GetHolder(Guid id) { using (var context = new BaseInfoDbContext(_connectionString)) { return(context.Holders .Include(h => h.Addresses).ThenInclude(a => a.PhoneNumbers) .Include(h => h.SocialNetworks) .Include(h => h.Tags) .FirstOrDefault(h => h.Id == id)); } }
public void AddHolder(Holder holder) { using (var context = new BaseInfoDbContext(_connectionString)) { if (context.Holders.Any(m => m.Moniker == holder.Moniker)) { throw new MonikerIsAlreadyExistException("Could not to add new hoilder with this moniker", holder.Moniker); } context.Holders.Add(holder); context.SaveChanges(); } }
public static dynamic GetDbSet <T>(this BaseInfoDbContext context, T entity) where T : class { foreach (var prop in context.GetType().GetProperties()) { var dbSetType = typeof(DbSet <>).MakeGenericType(entity.GetType()); if (dbSetType.IsAssignableFrom(prop.PropertyType)) { return(prop.GetValue(context)); } } return(null); }
public void DeleteHolder(string moniker) { using (var context = new BaseInfoDbContext(_connectionString)) { var holderToRemove = GetHolder(moniker); if (holderToRemove != null) { context.Remove(holderToRemove); context.Holders.Remove(holderToRemove); context.SaveChanges(); } } }
public static void CheckForRemove(this IEntity source, IEntity target, BaseInfoDbContext context) { foreach (var prop in source.GetType().GetProperties()) { if (typeof(IEnumerable <IEntity>).IsAssignableFrom(prop.PropertyType)) { var targetProp = target.GetType().GetProperty(prop.Name); if (targetProp == null) { continue; } var sourceValue = (IEnumerable <IEntity>)prop.GetValue(source); var targetValue = (IEnumerable <IEntity>)targetProp.GetValue(target); if (sourceValue == null || targetValue == null) { throw new NullReferenceException("Source value or target value is null"); } foreach (var child in sourceValue) { target = targetValue.FirstOrDefault(t => t.Id == child.Id); if (target == null) { continue; } child.CheckForRemove(target, context); } var forRemove = sourceValue.Except(targetValue, (x, y) => x.Id == y.Id); foreach (var item in forRemove) { var dbSet = context.GetDbSet(item); if (dbSet != null) { var itemToRemove = dbSet.Find(item.Id); context.Remove(itemToRemove); } } } } }
public void UpdateHolder(Holder holderData) { // Check input value if (holderData == null) { _logger.AddError("HolderData is null when call UpdateHolder method"); throw new ArgumentNullException("holderData", "HolderData is null when call UpdateHolder method"); } // Get current holder We need to update var holder = GetHolder(holderData.Id); // Check existing current holder if (holder == null) { _logger.AddError("Cannot to get holder for update"); throw new ArgumentNullException("holder", "Holder is null while update!"); } try { using (var context = new BaseInfoDbContext(_connectionString)) { holder.CheckForRemove(holderData, context); holder = holderData; context.Update(holder); context.SaveChanges(); } } catch (NullReferenceException ex) { _logger.AddError("Issue with updating holder"); _logger.AddError(ex.Message); throw new NullReferenceException("Cannot to update holder", ex); } }
public Holder GetHolder(string moniker) { try { using (var context = new BaseInfoDbContext(_connectionString)) { return(context.Holders .Include(h => h.Addresses).ThenInclude(a => a.PhoneNumbers) .Include(h => h.SocialNetworks) .Include(h => h.Tags) .First(h => h.Moniker == moniker)); } } catch (ArgumentNullException ex) { _logger.AddError(ex.Message); return(null); } catch (InvalidOperationException ex) { _logger.AddError(ex.Message); return(null); } }