Beispiel #1
0
        /// <summary>
        /// Gets the element by id.
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        /// 创建者:叶烨星
        /// 创建时间:2018/1/28 18:47
        /// 修改者:
        /// 修改时间:
        public ServiceResult <T> GetElementById(int id)
        {
            if (id <= 0)
            {
                return(new ServiceResult <T>
                {
                    State = false,
                    Message = "输入的id不合法"
                });
            }

            T element = db.Set <T>().Find(id);

            if (element == null)
            {
                return(new ServiceResult <T>
                {
                    State = false,
                    Message = "该元素不存在"
                });
            }

            return(new ServiceResult <T>
            {
                Result = element,
                State = true
            });
        }
Beispiel #2
0
 public static List <Clients> GetAllClients()
 {
     using (var context = new DataModelContainer())
     {
         List <Clients> clients = new List <Clients>();
         return(context.Set <Clients>().ToList());
     }
 }
        //实现对数据库的修改功能

        public bool UpdateEntity(T entity)
        {
            //EF4.0的写法

            //db.CreateObjectSet<T>().Addach(entity);

            //db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);

            //EF5.0的写法

            db.Set <T>().Attach(entity);

            db.Entry <T>(entity).State = EntityState.Modified;



            return(db.SaveChanges() > 0);
        }
        public virtual async Task <CommandResult <UserProfileData> > Create(UserProfileData data)
        {
            var entity = data.ToEntity();

            _context.Set(entity.GetType()).Attach(entity);
            _context.Entry(entity).State = System.Data.Entity.EntityState.Added;
            await _context.SaveChangesAsync();

            return(new CommandResult <UserProfileData>(data, new UserProfileData(entity)));
        }
        public virtual async Task <CommandResult <UserData> > Update(UserData data)
        {
            var entity = data.ToEntity();

            _context.Set(entity.GetType()).Attach(entity);
            _context.Entry(entity).State = System.Data.Entity.EntityState.Modified;
            var rows = await _context.SaveChangesAsync();

            return(new CommandResult <UserData>(data, new UserData(entity), rows > 0));
        }
        public virtual async Task <CommandResult <RoleData> > Delete(RoleData data)
        {
            var entity = data.ToEntity();

            _context.Set(entity.GetType()).Attach(entity);
            _context.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
            var rows = await _context.SaveChangesAsync();

            return(new CommandResult <RoleData>(data, null, rows > 0));
        }
Beispiel #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="item"></param>
        /// <returns>A saved entity, otherwise, same passed object.</returns>
        public T Add(T item)
        {
            try
            {
                Context.Set <T>().Attach(item);
                Context.Entry(item).State = EntityState.Added;
                Context.SecureSaveChanges();
            }
            catch (Exception ex)
            {
                var dataException = DataLayerExceptionFactory.Create(ex, item);
                throw dataException;
            }

            return(item);
        }
Beispiel #8
0
        //查询 whereLambda u=>u.id>0 条件
        public IQueryable <T> GetEntity(Expression <Func <T, bool> > whereLambda)
        {
            DataModelContainer db = new DataModelContainer();

            return(db.Set <T>().Where(whereLambda).AsQueryable());
        }