/// <summary> /// Inserts a given entity in the database. /// </summary> /// <param name="entity">The entity to be inserted.</param> public virtual void Insert(T entity) { using (var ctx = new LibraryDBContext()) { var dbSet = ctx.Set <T>(); dbSet.Add(entity); ctx.SaveChanges(); Log.Info("Entity of type " + entity.GetType().ToString() + " have been successfully inserted!"); } }
/// <summary> /// Updates a given entity in the database. /// </summary> /// <param name="item">The modified entity.</param> public virtual void Update(T item) { using (var ctx = new LibraryDBContext()) { var dbSet = ctx.Set <T>(); dbSet.Attach(item); ctx.Entry(item).State = EntityState.Modified; ctx.SaveChanges(); Log.Info("Entity of type " + item.GetType().ToString() + " have been successfully updated!"); } }
/// <summary> /// Deletes a given entity from the database. /// </summary> /// <param name="entityToDelete">The entity to delete.</param> public virtual void Delete(T entityToDelete) { using (var ctx = new LibraryDBContext()) { var dbSet = ctx.Set <T>(); if (ctx.Entry(entityToDelete).State == EntityState.Detached) { dbSet.Attach(entityToDelete); } dbSet.Remove(entityToDelete); ctx.SaveChanges(); Log.Info("Entity of type " + entityToDelete.GetType().ToString() + " have been successfully deleted!"); } }