コード例 #1
0
        public TEntity Delete <TEntity>(TEntity entity) where TEntity : class
        {
            if (entity == null)
            {
                return(null);
            }

            using (var context = new MailboxContext())
            {
                // Make sure DbContext is tracking this entry
                var entry = context.Entry(entity);
                if (entry.State == EntityState.Detached)
                {
                    context.Set <TEntity>().Attach(entity);
                }

                var deletedEntity = context.Set <TEntity>().Remove(entity);
                return(deletedEntity);
            }
        }
コード例 #2
0
        public IQueryable <TEntity> Get <TEntity>(Expression <Func <TEntity, bool> > filter = null) where TEntity : class
        {
            using (var context = new MailboxContext())
            {
                IQueryable <TEntity> query = context.Set <TEntity>();

                query = filter != null?query.Where(filter) : query;

                return(query);
            }
        }
コード例 #3
0
        public TEntity Add <TEntity>(TEntity entity) where TEntity : class
        {
            if (entity == null)
            {
                return(null);
            }

            using (var context = new MailboxContext())
            {
                var addedEntity = context.Set <TEntity>().Add(entity);
                context.SaveChanges();
                return(addedEntity);
            }
        }
コード例 #4
0
        public TEntity Update <TEntity>(TEntity entity) where TEntity : class
        {
            if (entity == null)
            {
                return(null);
            }

            using (var context = new MailboxContext())
            {
                // Make sure DbContext is tracking this entry
                var entry = context.Entry(entity);
                if (entry.State == EntityState.Detached)
                {
                    context.Set <TEntity>().Attach(entity);
                }

                entry.State = EntityState.Modified;
                context.SaveChanges();
                return(entity);
            }
        }