Exemplo n.º 1
0
        /// <summary>
        /// 删除所有的符合特定表达式的数据
        /// </summary>
        /// <param name="Predicate">查询条件谓语表达式</param>
        /// <param name="isSave">是否执行保存</param>
        /// <returns>操作影响行数</returns>
        public bool DeleteEntity(Expression <Func <TEntity, bool> > Predicate, bool isSave = true)
        {
            //删除实体 //查询出来一个删除
            HSJXEntities DBContext = GetDataContext();

            {
                List <TEntity> Entities = DBContext.Set <TEntity>().Where <TEntity>(Predicate).AsQueryable().ToList <TEntity>();
                foreach (var item in Entities)
                {
                    DBContext.Set <TEntity>().Attach(item);
                    DBContext.Entry <TEntity>(item).State = EntityState.Deleted;
                }
            }

            return(SaveCommit(isSave));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 插入实体记录集合
        /// </summary>
        /// <param name="entities">实体记录集合</param>
        /// <param name="isSave">是否知悉保存</param>
        /// <returns>操作影响行数</returns>
        public bool AddEntityByBluk(IEnumerable <TEntity> entities, bool isSave = true)
        {
            HSJXEntities DBContext = GetDataContext();

            entities = entities.ToArray();
            var conn = new SqlConnection(DBContext.Database.Connection.ConnectionString);

            conn.Open();
            Type t        = typeof(TEntity);
            var  bulkCopy = new SqlBulkCopy(conn)
            {
                DestinationTableName = t.Name
            };
            var properties = t.GetProperties().ToArray();
            var table      = new DataTable();

            foreach (var property in properties)
            {
                Type propertyType = property.PropertyType;
                if (propertyType.IsGenericType &&
                    propertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    propertyType = Nullable.GetUnderlyingType(propertyType);
                }

                table.Columns.Add(new DataColumn(property.Name, propertyType));
            }

            foreach (var entity in entities)
            {
                table.Rows.Add(properties.Select(
                                   property => GetPropertyValue(
                                       property.GetValue(entity, null))).ToArray());
            }
            //其实可以修改按照列名匹配写入DB
            bulkCopy.WriteToServer(table);
            conn.Close();
            return(true);
        }
Exemplo n.º 3
0
 public EFRepositoryBase()
 {
     //构造函数直接初始化
     EFContext = ServiceLocator.Current.GetInstance <HSJXEntities>();
 }
Exemplo n.º 4
0
 /// <summary>
 /// 数据处理,异常必须在内部进行处理
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public abstract DataMigrationResult DataHandle(HSJXEntities context);