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 <Beat>()
            .HasKey(x => x.Id);

            builder.Entity <Beat>()
            .HasOne(x => x.Producer)
            .WithMany(b => b.Beats)
            .HasForeignKey(x => x.ProducerId);

            builder.Entity <CloudinaryImage>()
            .HasOne(x => x.Uploader)
            .WithMany(i => i.Images)
            .HasForeignKey(x => x.UploaderId);

            builder.Entity <CloudinaryBeat>()
            .HasOne(x => x.Uploader)
            .WithMany(b => b.CloudinaryBeats)
            .HasForeignKey(x => x.UploaderId);

            builder.Entity <Follower>()
            .HasOne(m => m.User)
            .WithMany(m => m.Followers)
            .HasForeignKey(k => k.UserId);

            builder.Entity <Follower>()
            .HasOne(m => m.FollowedBy)
            .WithMany(m => m.Following)
            .HasForeignKey(k => k.FollowedById);

            builder.Entity <ApplicationUser>()
            .HasMany(x => x.Notifications)
            .WithOne(x => x.User)
            .HasForeignKey(x => x.UserId);
        }
示例#2
0
        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder
            .Entity <ApplicationUser>()
            .HasOne(u => u.Profile)
            .WithOne()
            .HasForeignKey <Profile>(p => p.UserId)
            .OnDelete(DeleteBehavior.Restrict);

            builder
            .Entity <Follow>()
            .HasOne(f => f.User)
            .WithMany(u => u.Followers)
            .HasForeignKey(f => f.UserId)
            .OnDelete(DeleteBehavior.Restrict);

            builder
            .Entity <Follow>()
            .HasOne(f => f.Follower)
            .WithMany()
            .HasForeignKey(f => f.FollowerId)
            .OnDelete(DeleteBehavior.Restrict);

            builder
            .Entity <Play>()
            .HasOne(p => p.Player)
            .WithMany()
            .HasForeignKey(p => p.PlayerId)
            .OnDelete(DeleteBehavior.Restrict);

            builder
            .Entity <Play>()
            .HasOne(p => p.Beat)
            .WithMany(b => b.Plays)
            .HasForeignKey(p => p.BeatId)
            .OnDelete(DeleteBehavior.Restrict);

            builder
            .Entity <Beat>()
            .HasOne(p => p.Producer)
            .WithMany(b => b.Beats)
            .HasForeignKey(p => p.ProducerId)
            .OnDelete(DeleteBehavior.Restrict);

            builder
            .Entity <Like>()
            .HasOne(l => l.Beat)
            .WithMany(b => b.Likes)
            .HasForeignKey(l => l.BeatId)
            .OnDelete(DeleteBehavior.Restrict);

            builder
            .Entity <Like>()
            .HasOne(l => l.User)
            .WithMany(u => u.Likes)
            .HasForeignKey(l => l.UserId)
            .OnDelete(DeleteBehavior.Restrict);

            builder
            .Entity <BeatComment>()
            .HasOne(c => c.Beat)
            .WithMany(b => b.Comments)
            .HasForeignKey(c => c.BeatId)
            .OnDelete(DeleteBehavior.Restrict);

            builder
            .Entity <BeatComment>()
            .HasOne(c => c.User)
            .WithMany()
            .HasForeignKey(c => c.UserId)
            .OnDelete(DeleteBehavior.Restrict);

            builder
            .Entity <ArtistComment>()
            .HasOne(c => c.Artist)
            .WithMany(a => a.Comments)
            .HasForeignKey(c => c.ArtistId)
            .OnDelete(DeleteBehavior.Restrict);

            builder
            .Entity <ArtistComment>()
            .HasOne(c => c.User)
            .WithMany()
            .HasForeignKey(c => c.UserId)
            .OnDelete(DeleteBehavior.Restrict);

            builder
            .Entity <Event>()
            .HasOne(e => e.Manager)
            .WithMany()
            .HasForeignKey(e => e.ManagerId)
            .OnDelete(DeleteBehavior.Restrict);

            builder
            .Entity <Event>()
            .Property(e => e.Price)
            .HasColumnType("decimal(18,4)");

            builder
            .Entity <Notification>()
            .HasOne(n => n.User)
            .WithMany(u => u.Notifications)
            .HasForeignKey(n => n.UserId)
            .OnDelete(DeleteBehavior.Restrict);

            builder
            .Entity <Notification>()
            .HasOne(n => n.Initiator)
            .WithMany()
            .HasForeignKey(n => n.InitiatorId)
            .OnDelete(DeleteBehavior.Restrict);

            // 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;
            }
        }