Ejemplo n.º 1
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasDefaultSchema(DbScheme);

            Type modelBuilderType = typeof(IModelBuilder);

            Type contextType = this.GetType();
            Type dbSetType   = typeof(DbSet <>);

            contextType
            .GetProperties(BindingFlags.Public | BindingFlags.Instance)
            .Where(t => t.PropertyType.IsGenericType && t.PropertyType.GetGenericTypeDefinition() == dbSetType && t.PropertyType.GetGenericArguments().Length == 1)
            .Select(t => t.PropertyType.GetGenericArguments()[0])
            .ToList()
            .ForEach(t =>
            {
                if (t.GetConstructors().Any(c => c.GetParameters().Length == 0))
                {
                    IModelBuilder builder = (IModelBuilder)Activator.CreateInstance(t);

                    builder.CreateRules(modelBuilder);
                }
                else
                {
                    throw new InvalidOperationException(nameof(t.Name));
                }
            });

            base.OnModelCreating(modelBuilder);
        }