Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuring Foreign Key properties in Annotation #7

Open
borisdj opened this issue Feb 1, 2017 · 6 comments
Open

Configuring Foreign Key properties in Annotation #7

borisdj opened this issue Feb 1, 2017 · 6 comments
Assignees

Comments

@borisdj
Copy link
Contributor

borisdj commented Feb 1, 2017

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?

@isukces
Copy link
Owner

isukces commented Feb 1, 2017

Yes. Relations definition language is not well designed. We make definition is on storage level so we're involved in tuning foreign keys. I wish I could define relations on more abstract level.

Moreover I don't know why in fluent API we have DeleteBehavior with only 3 values Restrict , SetNull and Cascade but CreateTableBuilder.ForeignKey (used with MigrationBuilder) has more possibilities

  • it has both ReferentialAction onUpdate and ReferentialAction onDelete
  • ReferentialAction has 5 values NoAction, Restrict, Cascade, SetNull, SetDefault

I have a plan to extend Shaman with possibility to define FK behaviour with attributes.
I hate reach-your-right-leg-to-the-left-ear code available with FluentApi

@borisdj
Copy link
Contributor Author

borisdj commented Feb 1, 2017

I noticed, enums should have the same values, or even they could have only one Enum for both usage.

Great, looking forward to it.
Write here once it's done and I'll test it, can help with writing Docs as well.

@borisdj
Copy link
Contributor Author

borisdj commented Feb 2, 2017

I have asked EF team on GitHub about DeleteBehavior, here is the issue:
dotnet/efcore#7530

@isukces isukces self-assigned this Feb 9, 2017
@isukces isukces closed this as completed Jul 11, 2017
@borisdj
Copy link
Contributor Author

borisdj commented Jul 11, 2017

How is this now being used, I can't find example of it in code nor in test.
Is it new attribute, named how? or extension of existing.

@isukces
Copy link
Owner

isukces commented Jul 11, 2017

Sorry. I will put some info on main web page.

@isukces isukces reopened this Jul 11, 2017
@borisdj
Copy link
Contributor Author

borisdj commented Jul 11, 2017

Thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants