Esempio n. 1
0
        /// <summary>
        /// 从数据库读取  必须有Key特性
        /// </summary>
        /// <param name="dataEntity">数据实体</param>
        public static bool Read <T>(this IDataEntity <T> dataEntity) where T : DbContext, new()
        {
            var    context    = dataEntity.GetDbContext();
            var    properties = dataEntity.GetType().GetProperties();
            var    result     = false;
            object model      = null;

            foreach (var item in properties)
            {
                if (item.GetCustomAttribute(typeof(KeyAttribute)) != null)
                {
                    var keyValue = item.GetValue(dataEntity);
                    model = context.Set(dataEntity.GetType()).Find(keyValue);
                    break;
                }
            }
            if (model != null)
            {
                result = true;
                foreach (var item in properties)
                {
                    item.SetValue(dataEntity, item.GetValue(model));
                }
            }
            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// 更新数据实体值到数据库
        ///备注: 根据当前数据对象的值向数据库插入一条数据库记录。如果没有为对象添加任何属性,则不会产生任何效果。
        /// </summary>
        /// <param name="dataEntity">数据实体</param>
        public static void Update <T>(this IDataEntity <T> dataEntity) where T : DbContext, new()
        {
            var context = dataEntity.GetDbContext();

            context.Set(dataEntity.GetType()).Attach(dataEntity);
            context.Entry(dataEntity).State = EntityState.Modified;
            context.SaveChanges();
        }
Esempio n. 3
0
        /// <summary>
        /// 判断数据实体是否在数据库中存在 必须有Key特性
        /// </summary>
        /// <param name="dataEntity">数据实体</param>
        public static bool ExistsInDb <T>(this IDataEntity <T> dataEntity) where T : DbContext, new()
        {
            var context    = dataEntity.GetDbContext();
            var properties = dataEntity.GetType().GetProperties();

            foreach (var item in properties)
            {
                if (item.GetCustomAttribute(typeof(KeyAttribute)) != null)
                {
                    var keyValue = item.GetValue(dataEntity);
                    var model    = context.Set(dataEntity.GetType()).Find(keyValue);
                    if (model == null)
                    {
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
            }
            throw new Exception("未找到实体类主键");
        }
Esempio n. 4
0
 /// <summary>
 /// 实体查询
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dataEntity">数据实体</param>
 /// <param name="where">数据查询条件</param>
 /// <param name="skip"></param>
 /// <param name="count"></param>
 /// <returns></returns>
 public static IQueryable <TEntity> Select <TEntity, T>(this IDataEntity <T> dataEntity, Expression <Func <TEntity, bool> > where, int skip, int count) where T : DbContext, new() where TEntity : class, IDataEntity <T>, new()
 {
     return(dataEntity.GetDbContext().Set <TEntity>().Where(where).Skip(skip).Take(count));
 }
Esempio n. 5
0
 /// <summary>
 /// 实体查询
 /// </summary>
 /// <typeparam name="T">实体类型</typeparam>
 /// <param name="dataEntity">数据实体</param>
 /// <returns>符合条件的数据对象</returns>
 public static IQueryable <TEntity> Select <TEntity, T>(this IDataEntity <T> dataEntity) where T : DbContext, new() where TEntity : class, IDataEntity <T>, new()
 {
     return(dataEntity.GetDbContext().Set <TEntity>().Where(m => true));
 }