Пример #1
0
 public IDetachedSet Set(Type entityType)
 {
     return(_setCache.GetOrAdd(entityType, t =>
     {
         Type setType = typeof(IDetachedSet <>).MakeGenericType(entityType);
         IDetachedSet detachedSet = (IDetachedSet)_detachedServices.ServiceProvider.GetRequiredService(setType);
         return detachedSet;
     }));
 }
        public static async Task SeedFromJsonFileAsync(this IDetachedContext context, string filePath, bool identityInsert = true)
        {
            string  json     = File.ReadAllText(filePath);
            JObject jsonRoot = (JObject)JsonConvert.DeserializeObject(json);

            foreach (var jsonCatalog in jsonRoot.Properties())
            {
                IDetachedSet set = context.Set(jsonCatalog.Name);

                // insert objects (detached roots), one by one.
                foreach (var jsonEntity in jsonCatalog.Values())
                {
                    object entity = jsonEntity.ToObject(set.EntityType);
                    await set.UpdateAsync(entity);
                }

                IEntityType entityType = context.DbContext.Model.FindEntityType(set.EntityType);
                if (identityInsert && HasIdentity(entityType))
                {
                    string tableName = entityType.Relational().TableName; // SQL table name.

                    // enable identity insert.
                    await context.DbContext.Database.OpenConnectionAsync();

                    await context.DbContext.Database.ExecuteSqlCommandAsync($"SET IDENTITY_INSERT [dbo].[{tableName}] ON;");

                    // save changes with identity insert enabled.
                    await context.SaveChangesAsync();

                    await context.DbContext.Database.ExecuteSqlCommandAsync($"SET IDENTITY_INSERT [dbo].[{tableName}] OFF;");

                    context.DbContext.Database.CloseConnection();
                }
                else
                {
                    await context.SaveChangesAsync();
                }
            }
        }
        /// <summary>
        /// Loads a page of the specified entity, and projects it to the given type.
        /// </summary>
        /// <typeparam name="TEntity">Clr type of the entity.</typeparam>
        /// <typeparam name="TProjection">Clr type of the result object.</typeparam>
        /// <param name="detachedSet">The detached context.</param>
        /// <param name="request">The request to load.</param>
        /// <param name="projection">The projection function.</param>
        /// <returns>A projected and paged result.</returns>
        public static async Task <IPage <TProjection> > LoadPageAsync <TEntity, TProjection>(this IDetachedSet <TEntity> detachedSet, int pageIndex, int pageSize, Func <IQueryable <TEntity>, IQueryable <TProjection> > configureQuery = null)
            where TEntity : class
            where TProjection : class
        {
            IPage <TProjection> result = new Page <TProjection>();

            result.PageIndex = Math.Max(1, pageIndex);
            result.PageSize  = pageSize;

            IQueryable <TEntity>     query      = detachedSet.GetBaseQuery();
            IQueryable <TProjection> finalQuery = configureQuery?.Invoke(query);

            result.RowCount = await finalQuery.CountAsync();

            result.PageCount = (int)Math.Ceiling((result.RowCount / Math.Max(result.PageSize, 1.0)));

            if (result.PageSize > 1)
            {
                finalQuery = finalQuery.Skip((pageIndex - 1) * pageSize)
                             .Take(pageSize);
            }

            result.Items = await finalQuery.ToListAsync();

            return(result);
        }