Beispiel #1
0
        /// <summary>
        /// 异步使用附带新值的实体更新指定实体属性的值,此方法不支持事务
        /// </summary>
        /// <param name="propertyExpresion">属性表达式,提供要更新的实体属性</param>
        /// <param name="entities">附带新值的实体属性,必须包含主键</param>
        /// <returns>操作影响的行数</returns>
        /// <exception cref="System.NotSupportedException"></exception>
        public async Task <int> UpdateAsync(Expression <Func <TEntity, object> > propertyExpresion, params TEntity[] entities)
        {
            propertyExpresion.CheckNotNull("propertyExpresion");
            entities.CheckNotNull("entities");
            CodeFirstDbContext context = new CodeFirstDbContext();

            context.Update <TEntity, TKey>(propertyExpresion, entities);
            bool fail;

            try
            {
                return(await context.SaveChangesAsync(false));
            }
            catch (DbUpdateConcurrencyException)
            {
                fail = true;
            }
            if (fail)
            {
                TKey[] ids = entities.Select(m => m.Id).ToArray();
                context.Set <TEntity>().Where(m => ids.Contains(m.Id)).Load();
                context.Update <TEntity, TKey>(propertyExpresion, entities);
                return(await context.SaveChangesAsync(false));
            }
            return(0);
        }
Beispiel #2
0
        /// <summary>
        /// 使用附带新值的实体更新指定实体属性的值,此方法不支持事务
        /// </summary>
        /// <param name="propertyExpresion">属性表达式,提供要更新的实体属性</param>
        /// <param name="entities">附带新值的实体属性,必须包含主键</param>
        /// <returns>操作影响的行数</returns>
        /// <exception cref="System.NotSupportedException"></exception>
        public int Update(Expression <Func <TEntity, object> > propertyExpresion, params TEntity[] entities)
        {
            propertyExpresion.CheckNotNull("propertyExpresion");
            entities.CheckNotNull("entities");
            CodeFirstDbContext context = new CodeFirstDbContext();

            context.Update <TEntity, TKey>(propertyExpresion, entities);
            try
            {
                return(context.SaveChanges(false));
            }
            catch (DbUpdateConcurrencyException)
            {
                TKey[] ids = entities.Select(m => m.Id).ToArray();
                context.Set <TEntity>().Where(m => ids.Contains(m.Id)).Load();
                context.Update <TEntity, TKey>(propertyExpresion, entities);
                return(context.SaveChanges(false));
            }
        }
        /// <summary>
        /// 设置数据库初始化,策略为自动迁移到最新版本
        /// </summary>
        public static void Initialize()
        {
            CodeFirstDbContext context = new CodeFirstDbContext();
            IDatabaseInitializer <CodeFirstDbContext> initializer;

            if (!context.Database.Exists())
            {
                initializer = new CreateDatabaseIfNotExistsWithSeed();
            }
            else
            {
                initializer = new MigrateDatabaseToLatestVersion <CodeFirstDbContext, MigrationsConfiguration>();
            }
            Database.SetInitializer(initializer);

            //EF预热,解决EF6第一次加载慢的问题
            ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
            StorageMappingItemCollection mappingItemCollection = (StorageMappingItemCollection)objectContext.ObjectStateManager
                                                                 .MetadataWorkspace.GetItemCollection(DataSpace.CSSpace);

            mappingItemCollection.GenerateViews(new List <EdmSchemaError>());
            context.Dispose();
        }