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

How to configure ON UPDATE on Foreign Key constraint using Fluent API #807

Closed
coultonluke opened this issue Jul 30, 2019 · 3 comments
Closed
Assignees

Comments

@coultonluke
Copy link

coultonluke commented Jul 30, 2019

Steps to reproduce

I have the following configuration for a foreign key relationship for one of my tables, I have configured the ON DELETE value:

builder.HasOne(t => t.Quiz)
    .WithMany(t => t.MostActiveQuizzes)
    .HasForeignKey(d => d.QuizId)
    .HasConstraintName("FK_MostActiveQuiz_Quiz_QuizId")
    .OnDelete(DeleteBehavior.Cascade);

However, I can't configure the ON UPDATE property in the same way, for illustration purposes:

builder.HasOne(t => t.Quiz)
    .WithMany(t => t.MostActiveQuizzes)
    .HasForeignKey(d => d.QuizId)
    .HasConstraintName("FK_MostActiveQuiz_Quiz_QuizId")
    .OnDelete(DeleteBehavior.Cascade)
    .OnUpdate(UpdateBehavior.Cascade); // ???

The issue

It is possible to configure the ON DELETE value for a foreign key using the Fluent API, but it doesn't appear to be possible to configure the ON UPDATE value.

Is there a way to do this, or is there a reason that this isn't possible?

I could always add these in to the migration code that has been created, but I'm not sure whether that's a good thing to do...

Further technical details

MySQL version: 5.7.24
Operating system: Windows 10
Pomelo.EntityFrameworkCore.MySql version: 2.2.0
Microsoft.AspNetCore.App version:

Other details about my project setup:

@lauxjpn lauxjpn self-assigned this Oct 4, 2019
@lauxjpn
Copy link
Collaborator

lauxjpn commented Oct 5, 2019

There is no support for ON UPDATE in the model builder of EF Core at the moment. There is however support for ON UPDATE for migrations according to this answer on StackOverflow.

(Most of the time ON UPDATE CASCADE can be considered a design smell and could be an indication to redesign the model, as primary keys should generally not be changed after they have been set.)

@ivanmem
Copy link

ivanmem commented Nov 15, 2023

Can you add support for at least configuration in the model? I don’t want to change it manually through phpmyadmin.
image
My project does not require good architecture.

@roji
Copy link

roji commented Nov 15, 2023

@ivanmem that requires EF-side support (tracked by dotnet/efcore#21563). You should be able to edit your migration and integrated the necessary DDL to add the ON UPDATE behavior (migration docs).

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

No branches or pull requests

4 participants