-
Notifications
You must be signed in to change notification settings - Fork 9
Description
In EF core default behavior for FK is that it automatically generates Index for it and set DeleteBehavior to Cascade.
Both are practical but sometime you want to change it, which can be done in Migration file after it is generated.
Another way, better, would be to configure it before generating migration, so in fluentAPI we have:
a) OnDelete method
modelBuilder.Entity<Movie>().HasOne(p => p.Genre) .WithMany(b => b.Movies) .OnDelete(DeleteBehavior.Restrict);
b) and to remove Index from FK Column.
var index = (Index)modelBuilder.Entity<MyEntity>().HasIndex(e => e.Name).Metadata;
index.DeclaringEntityType.RemoveIndex(index.Properties, runConventions: false);
However it would be best if these could be configured in Annotation, since goal is all in one place.
I would give Cascade feature higher priority over HasIndex when it comes to implementation, if they would not be implemented together.
One approach that comes to my mind at the moment is to use [ForeignKey] attribute, and to extend it somehow. Maybe make partial class [ForeignKeyAttribute] with another constructor
(string name, DeleteBehavior deleteBehavior, hasIndex = true).
Or make new attribute for this named [ForeignKeyExtendedAttribute] or what ever name suites best:
(string name, DeleteBehavior deleteBehavior = DeleteBehavior.Cascade, hasIndex = true).
Do you think this could also, relativly easily, be added to Shaman library?