Ejemplo n.º 1
0
        private async Task SeedEntities(StoreContext context)
        {
            foreach (var(jsonFile, entityType) in SeedDictionary)
            {
                var     tableName       = entityType.Name + "s";
                var     productProperty = context.GetType().GetTypeInfo().GetDeclaredProperty(tableName);
                dynamic productEntity   = productProperty?.GetValue(context);
                if (!((IQueryable)productEntity) !.Any())
                {
                    var jsonData = await File.ReadAllTextAsync(GetFileFullPath(jsonFile));

                    var     jsonSerializerType = typeof(JsonSerializerManager <>).MakeGenericType(entityType);
                    dynamic jsonSerializer     = Activator.CreateInstance(jsonSerializerType);
                    jsonSerializer !.JsonOptions = new JsonSerializerOptions();
                    var items = jsonSerializer.GenerateArray(jsonData);
                    foreach (var item in items !)
                    {
                        productEntity !.Add(item);
                    }

                    await context.SqlServerSaveChangesAsync(tableName);
                }
            }
        }