Exemple #1
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Seed();

            /// <summary>
            /// Set Shadow Property For All BaseExtendedEntity
            /// </summary>
            //foreach (var entityType in modelBuilder.Model.GetEntityTypes())
            //{
            //    if (typeof(BaseExtendedEntity).IsAssignableFrom(entityType.ClrType))
            //    {
            //        entityType.AddProperty("InsertDateTime", typeof(DateTime));
            //    }
            //}

            modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());

            /// <summary>
            /// Set Global Query Filter For All BaseExtendedEntity
            /// </summary>
            GetEntityTypes().ForEach(type =>
            {
                var method = SetGlobalQueryMethod.MakeGenericMethod(type);
                method.Invoke(this, new object[] { modelBuilder });
            });

            base.OnModelCreating(modelBuilder);
        }
Exemple #2
0
        private void SetGlobalIsDeletedFilter(ModelBuilder modelBuilder)
        {
            var entityTypes = GetType().Assembly.DefinedTypes.Where(x => x.BaseType == typeof(Entity)).Select(x => x.AsType()).ToList();

            foreach (var type in entityTypes)
            {
                var method = SetGlobalQueryMethod.MakeGenericMethod(type);
                method.Invoke(this, new object[] { modelBuilder });
            }
        }
Exemple #3
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            foreach (Type type in GetEntityTypes())
            {
                MethodInfo method = SetGlobalQueryMethod.MakeGenericMethod(type);
                method.Invoke(this, new object[] { modelBuilder });
            }

            base.OnModelCreating(modelBuilder);
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            foreach (var type in GetEntityTypes())
            {
                var method = SetGlobalQueryMethod.MakeGenericMethod(type);
                method.Invoke(this, new object[] { modelBuilder });
            }

            this.RegisterSP(modelBuilder);
            base.OnModelCreating(modelBuilder);
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ApplyConfiguration(new BlogConfiguration());
            modelBuilder.ApplyConfiguration(new PostConfiguration());

            foreach (var type in _entityTypeProvider.GetEntityTypes())
            {
                var method = SetGlobalQueryMethod.MakeGenericMethod(type);
                method.Invoke(this, new object[] { modelBuilder });
            }
            base.OnModelCreating(modelBuilder);
        }
        private void SetUpGlobalFilters(ModelBuilder builder)
        {
            var dbTypes = Assembly.GetExecutingAssembly().GetTypes()
                          .Where(t => t != typeof(DatabaseEntity) && typeof(DatabaseEntity).IsAssignableFrom(t))
                          .ToList();

            foreach (var type in dbTypes)
            {
                var method = SetGlobalQueryMethod.MakeGenericMethod(type);
                method.Invoke(this, new object[] { builder });
            }
        }
Exemple #7
0
        protected override void OnModelCreating(ModelBuilder builder)
        {
            string tableName = string.Empty;

            foreach (var item in GetReferencingAssemblies())
            {
                var method = SetGlobalQueryMethod.MakeGenericMethod(item);
#if Debug
                Debug.WriteLine("Adding global query for: " + typeof(T));
#endif
                method.Invoke(this, new object[] { builder });
            }
            base.OnModelCreating(builder);
        }
Exemple #8
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity <Playlist>().HasKey(e => e.Id);
            modelBuilder.Entity <Playlist>().HasQueryFilter(e => !e.IsDeleted);
            modelBuilder.Entity <Song>().HasKey(e => e.Id);
            modelBuilder.Entity <Song>().HasQueryFilter(e => !e.IsDeleted);

            foreach (var type in _entityTypeProvider.GetEntityTypes())
            {
                var method = SetGlobalQueryMethod.MakeGenericMethod(type);
                method.Invoke(this, new object[] { modelBuilder });
            }

            base.OnModelCreating(modelBuilder);
        }
Exemple #9
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            foreach (var type in modelBuilder.Model.GetEntityTypes())
            {
                var clrType = type.ClrType;


                if (clrType.BaseType.IsClass && clrType.BaseType.Name.Contains("BaseEntity"))
                {
                    var method = SetGlobalQueryMethod.MakeGenericMethod(clrType);
                    method.Invoke(this, new object[] { modelBuilder });
                }
            }
        }
Exemple #10
0
        protected override void OnModelCreating(ModelBuilder builder)
        {
            var navigation = builder.Entity <ProductCategory>()
                             .Metadata
                             .FindNavigation(nameof(ProductCategory.Products));

            navigation.SetPropertyAccessMode(PropertyAccessMode.Field);

            foreach (var type in GetEntityTypes())
            {
                var method = SetGlobalQueryMethod.MakeGenericMethod(type);
                method.Invoke(this, new object[] { builder });
            }

            base.OnModelCreating(builder);
        }
Exemple #11
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
            {
                relationship.DeleteBehavior = DeleteBehavior.Restrict;
            }

            //modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.OneToManyCascadeDeleteConvention>();
            modelBuilder.AddEntityConfigurationsFromAssembly(GetType().Assembly);

            foreach (var type in modelBuilder.Model.GetEntityTypes())
            {
                var method = SetGlobalQueryMethod.MakeGenericMethod(type.ClrType);
                method.Invoke(this, new object[] { modelBuilder });
            }

            base.OnModelCreating(modelBuilder);
        }
Exemple #12
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            #region entities configuration

            modelBuilder.ApplyConfiguration(new ProjectTagConfiguration());
            modelBuilder.ApplyConfiguration(new RelationTagConfiguration());

            #endregion

            foreach (var type in _entityTypeProvider.GetEntityTypes())
            {
                var method = SetGlobalQueryMethod.MakeGenericMethod(type);
                method.Invoke(this, new object[] { modelBuilder });
            }

            base.OnModelCreating(modelBuilder);

            #region Soft delete configuration

            /*
             * foreach (var entityType in modelBuilder.Model.GetEntityTypes())
             * {
             *
             *  // 1. Add the IsDeleted property
             *  entityType.GetOrAddProperty("IsDeleted", typeof(bool));
             *
             *  // 2. Create the query filter
             *  var parameter = Expression.Parameter(entityType.ClrType);
             *
             *  // EF.Property<bool>(post, "IsDeleted")
             *  var propertyMethodInfo = typeof(EF).GetMethod("Property").MakeGenericMethod(typeof(bool));
             *  var isDeletedProperty = Expression.Call(propertyMethodInfo, parameter, Expression.Constant("IsDeleted"));
             *
             *  // EF.Property<bool>(post, "IsDeleted") == false
             *  BinaryExpression compareExpression = Expression.MakeBinary(ExpressionType.Equal, isDeletedProperty, Expression.Constant(false));
             *
             *  // post => EF.Property<bool>(post, "IsDeleted") == false
             *  var lambda = Expression.Lambda(compareExpression, parameter);
             *
             *  modelBuilder.Entity(entityType.ClrType).HasQueryFilter(lambda);
             * }
             */
            #endregion
        }
Exemple #13
0
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            //https://github.com/aspnet/EntityFramework/issues/2805
            builder.AddEntityConfigurationsFromAssembly(GetType().GetTypeInfo().Assembly);

            //http://gunnarpeipman.com/2017/08/ef-core-global-query-filters/
            //foreach (var type in _entityTypeProvider.GetEntityTypes())
            //{
            //    var method = SetGlobalQueryMethod.MakeGenericMethod(type);
            //    method.Invoke(this, new object[] { builder });
            //}

            foreach (var type in GetEntityTypes())
            {
                var method = SetGlobalQueryMethod.MakeGenericMethod(type);
                method.Invoke(this, new object[] { builder });
            }
        }
Exemple #14
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            //modelBuilder.Entity<Product>(product => {
            //    product.HasKey(p => p.Id).IsClustered(false);
            //    product.Property("Id").ValueGeneratedOnAdd();
            //});

            foreach (var type in modelBuilder.Model.GetEntityTypes())
            {
                var clrType = type.ClrType;


                if (clrType.BaseType.IsClass && clrType.BaseType.Name.Contains("BaseEntity"))
                {
                    var method = SetGlobalQueryMethod.MakeGenericMethod(clrType);
                    method.Invoke(this, new object[] { modelBuilder });
                }
            }
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity <AspNetRoleClaims>(entity =>
            {
                entity.HasIndex(e => e.RoleId);

                entity.Property(e => e.Id).ValueGeneratedNever();

                entity.Property(e => e.RoleId).IsRequired();

                entity.HasOne(d => d.Role)
                .WithMany(p => p.AspNetRoleClaims)
                .HasForeignKey(d => d.RoleId);
            });

            modelBuilder.Entity <AspNetRoles>(entity =>
            {
                entity.HasIndex(e => e.NormalizedName)
                .HasName("RoleNameIndex")
                .IsUnique();

                entity.Property(e => e.Id).ValueGeneratedNever();

                entity.Property(e => e.Name).HasMaxLength(256);

                entity.Property(e => e.NormalizedName).HasMaxLength(256);
            });

            modelBuilder.Entity <AspNetUserClaims>(entity =>
            {
                entity.HasIndex(e => e.UserId);

                entity.Property(e => e.Id).ValueGeneratedNever();

                entity.Property(e => e.UserId).IsRequired();

                entity.HasOne(d => d.User)
                .WithMany(p => p.AspNetUserClaims)
                .HasForeignKey(d => d.UserId);
            });

            modelBuilder.Entity <AspNetUserLogins>(entity =>
            {
                entity.HasKey(e => new { e.LoginProvider, e.ProviderKey });

                entity.HasIndex(e => e.UserId);

                entity.Property(e => e.UserId).IsRequired();

                entity.HasOne(d => d.User)
                .WithMany(p => p.AspNetUserLogins)
                .HasForeignKey(d => d.UserId);
            });

            modelBuilder.Entity <AspNetUserRoles>(entity =>
            {
                entity.HasKey(e => new { e.UserId, e.RoleId });

                entity.HasIndex(e => e.RoleId);

                entity.HasOne(d => d.Role)
                .WithMany(p => p.AspNetUserRoles)
                .HasForeignKey(d => d.RoleId);

                entity.HasOne(d => d.User)
                .WithMany(p => p.AspNetUserRoles)
                .HasForeignKey(d => d.UserId);
            });

            modelBuilder.Entity <AspNetUsers>(entity =>
            {
                entity.HasIndex(e => e.NormalizedEmail)
                .HasName("EmailIndex");

                entity.HasIndex(e => e.NormalizedUserName)
                .HasName("UserNameIndex")
                .IsUnique();

                entity.Property(e => e.Id).ValueGeneratedNever();

                entity.Property(e => e.Email).HasMaxLength(256);

                entity.Property(e => e.NormalizedEmail).HasMaxLength(256);

                entity.Property(e => e.NormalizedUserName).HasMaxLength(256);

                entity.Property(e => e.UserName).HasMaxLength(256);
            });

            modelBuilder.Entity <AspNetUserTokens>(entity =>
            {
                entity.HasKey(e => new { e.UserId, e.LoginProvider, e.Name });

                entity.HasOne(d => d.User)
                .WithMany(p => p.AspNetUserTokens)
                .HasForeignKey(d => d.UserId);
            });

            modelBuilder.Entity <Customer>(entity =>
            {
                entity.HasIndex(e => new { e.LastName, e.FirstName })
                .HasName("IndexCustomerName");

                entity.Property(e => e.City).HasMaxLength(40);

                entity.Property(e => e.Country).HasMaxLength(40);

                entity.Property(e => e.FirstName)
                .IsRequired()
                .HasMaxLength(40);

                entity.Property(e => e.LastName)
                .IsRequired()
                .HasMaxLength(40);

                entity.Property(e => e.Phone).HasMaxLength(20);
            });

            modelBuilder.Entity <Order>(entity =>
            {
                entity.HasIndex(e => e.CustomerId)
                .HasName("IndexOrderCustomerId");

                entity.HasIndex(e => e.OrderDate)
                .HasName("IndexOrderOrderDate");

                entity.Property(e => e.OrderDate)
                .HasColumnType("datetime")
                .HasDefaultValueSql("(getdate())");

                entity.Property(e => e.OrderNumber).HasMaxLength(10);

                entity.Property(e => e.TotalAmount)
                .HasColumnType("decimal(12, 2)")
                .HasDefaultValueSql("((0))");

                entity.HasOne(d => d.Customer)
                .WithMany(p => p.Order)
                .HasForeignKey(d => d.CustomerId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_ORDER_REFERENCE_CUSTOMER");
            });

            modelBuilder.Entity <OrderItem>(entity =>
            {
                entity.HasIndex(e => e.OrderId)
                .HasName("IndexOrderItemOrderId");

                entity.HasIndex(e => e.ProductId)
                .HasName("IndexOrderItemProductId");

                entity.Property(e => e.Quantity).HasDefaultValueSql("((1))");

                entity.Property(e => e.UnitPrice)
                .HasColumnType("decimal(12, 2)")
                .HasDefaultValueSql("((0))");

                entity.HasOne(d => d.Order)
                .WithMany(p => p.OrderItem)
                .HasForeignKey(d => d.OrderId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_ORDERITE_REFERENCE_ORDER");

                entity.HasOne(d => d.Product)
                .WithMany(p => p.OrderItem)
                .HasForeignKey(d => d.ProductId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_ORDERITE_REFERENCE_PRODUCT");
            });

            modelBuilder.Entity <Product>(entity =>
            {
                entity.HasIndex(e => e.ProductName)
                .HasName("IndexProductName");

                entity.HasIndex(e => e.SupplierId)
                .HasName("IndexProductSupplierId");

                entity.Property(e => e.Package).HasMaxLength(30);

                entity.Property(e => e.ProductName)
                .IsRequired()
                .HasMaxLength(50);

                entity.Property(e => e.UnitPrice)
                .HasColumnType("decimal(12, 2)")
                .HasDefaultValueSql("((0))");

                entity.HasOne(d => d.Supplier)
                .WithMany(p => p.Product)
                .HasForeignKey(d => d.SupplierId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_PRODUCT_REFERENCE_SUPPLIER");
            });

            modelBuilder.Entity <Supplier>(entity =>
            {
                entity.HasIndex(e => e.CompanyName)
                .HasName("IndexSupplierName");

                entity.HasIndex(e => e.Country)
                .HasName("IndexSupplierCountry");

                entity.Property(e => e.City).HasMaxLength(40);

                entity.Property(e => e.CompanyName)
                .IsRequired()
                .HasMaxLength(40);

                entity.Property(e => e.ContactName).HasMaxLength(50);

                entity.Property(e => e.ContactTitle).HasMaxLength(40);

                entity.Property(e => e.Country).HasMaxLength(40);

                entity.Property(e => e.Fax).HasMaxLength(30);

                entity.Property(e => e.Phone).HasMaxLength(30);
            });

            foreach (var type in GetEntityTypes())
            {
                var method = SetGlobalQueryMethod.MakeGenericMethod(type);
                method.Invoke(this, new object[] { modelBuilder });
            }
        }