コード例 #1
0
        protected override void OnModelCreating(ModelBuilder builder)
        {
            // Needed for Identity models configuration
            base.OnModelCreating(builder);

            this.ConfigureUserIdentityRelations(builder);

            EntityIndexesConfiguration.Configure(builder);

            var entityTypes = builder.Model.GetEntityTypes().ToList();

            // Set global query filter for not deleted entities only
            var deletableEntityTypes = entityTypes
                                       .Where(et => et.ClrType != null && typeof(IDeletableEntity).IsAssignableFrom(et.ClrType));

            foreach (var deletableEntityType in deletableEntityTypes)
            {
                var method = SetIsDeletedQueryFilterMethod.MakeGenericMethod(deletableEntityType.ClrType);
                method.Invoke(null, new object[] { builder });
            }

            // Disable cascade delete
            var foreignKeys = entityTypes
                              .SelectMany(e => e.GetForeignKeys().Where(f => f.DeleteBehavior == DeleteBehavior.Cascade));

            foreach (var foreignKey in foreignKeys)
            {
                foreignKey.DeleteBehavior = DeleteBehavior.Restrict;
            }

            builder
            .Entity <DealsProductsQuantity>()
            .HasKey(dp => new { dp.DealId, dp.ProductId });
            builder
            .Entity <DealsProductsQuantity>()
            .HasOne(dp => dp.Deal)
            .WithMany(d => d.Products)
            .HasForeignKey(dp => dp.DealId);

            builder
            .Entity <DealsProductsQuantity>()
            .HasOne(dp => dp.Product)
            .WithMany(p => p.Deals)
            .HasForeignKey(dp => dp.ProductId);

            builder
            .Entity <Account>()
            .HasMany(a => a.Deals)
            .WithOne(d => d.Account)
            .OnDelete(DeleteBehavior.Cascade);
            builder
            .Entity <Deal>()
            .HasOne(d => d.Account)
            .WithMany(a => a.Deals)
            .HasForeignKey(d => d.AccountId);
        }
コード例 #2
0
        protected override void OnModelCreating(ModelBuilder builder)
        {
            // Needed for Identity models configuration
            base.OnModelCreating(builder);

            this.ConfigureUserIdentityRelations(builder);

            EntityIndexesConfiguration.Configure(builder);

            builder.Entity <Organization>()
            .HasOne(o => o.User)
            .WithOne(u => u.Organization)
            .HasForeignKey <ApplicationUser>(u => u.OrganizationId);


            var entityTypes = builder.Model.GetEntityTypes().ToList();

            // Set global query filter for not deleted entities only
            var deletableEntityTypes = entityTypes
                                       .Where(et => et.ClrType != null && typeof(IDeletableEntity).IsAssignableFrom(et.ClrType));

            foreach (var deletableEntityType in deletableEntityTypes)
            {
                var method = SetIsDeletedQueryFilterMethod.MakeGenericMethod(deletableEntityType.ClrType);
                method.Invoke(null, new object[] { builder });
            }

            // Disable cascade delete
            var foreignKeys = entityTypes
                              .SelectMany(e => e.GetForeignKeys().Where(f => f.DeleteBehavior == DeleteBehavior.Cascade));

            foreach (var foreignKey in foreignKeys)
            {
                foreignKey.DeleteBehavior = DeleteBehavior.Restrict;
            }
        }