Beispiel #1
0
        public async Task SeedAsync(DataSeederType type)
        {
            // Get set method
            var seederList    = GetAllSeeder(type);
            var contextType   = _context.GetType();
            var setMethodInfo = contextType.GetMethod(nameof(_context.Set), new Type[0]);

            var constructorArguments = new object[] { _serviceProvider };

            foreach (var seederType in seederList)
            {
                // Create generic set method
                var entityType = seederType.GetInterfaces().First().GetGenericArguments()[0];
                var setMethod  = setMethodInfo.MakeGenericMethod(entityType);
                var set        = (IQueryable <object>)setMethod.Invoke(_context, null);

                if (!set.Any())
                {
                    // Get seed method
                    var entityInterfaceType = typeof(DataSeederBase <,>).MakeGenericType(entityType);
                    var seedMethod          = entityInterfaceType.GetMethod(nameof(DataSeederBase <EntityBase <TId>, TId> .Seed));

                    // Create seeder and call the method on it
                    var seederObject = Activator.CreateInstance(seederType, constructorArguments);
                    seedMethod.Invoke(seederObject, null);
                }
            }

            await _context.SaveAllChangesAsync();
        }