3

As the title says, I'm trying to delete an object using Entity Framework, MySQL and CodeFirst.

I have a many-to-many relations in my object, and I only remove one convention:

modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

in my context. But I still get this when trying to delete:

Cannot delete or update a parent row: a foreign key constraint fails (`db`.`jagtartjagtrejse`, CONSTRAINT `JagtArt_JagtRejser_Target` FOREIGN KEY (`JagtRejse_JagtRejseID`) REFERENCES `jagtrejse` (`JagtRejseID`))

Is on delete cascade not supported in mysql connector, or is something wrong?

Fischermaen
  • 12,238
  • 2
  • 39
  • 56
Dynde
  • 2,592
  • 4
  • 33
  • 56
  • These questions may help http://stackoverflow.com/questions/3334619/cannot-delete-or-update-a-parent-row-a-foreign-key-constraint-fails, http://stackoverflow.com/questions/1905470/cannot-delete-or-update-a-parent-row-a-foreign-key-constraint-fails – Eranga Jan 13 '12 at 09:47
  • Thank you, but they do not unfortunately, as it's related to Entity Framework and CodeFirst specifically, I don't (and shouldn't) touch any SQL DDL. – Dynde Jan 13 '12 at 09:52

2 Answers2

0

https://stackoverflow.com/a/5532950/965013

I believe this answers your question. The link shows how to disable cascade on delete, but as you will see, turning it on is simply a matter of calling

modelBuilder.Entity<...>() 
        .HasRequired(...) 
        .WithMany(...) 
        .HasForeignKey(...) 
        .WillCascadeOnDelete(true); 
Community
  • 1
  • 1
0

By forcing cascadeDelete: true in EF migration file, I get the same result.

ON DELETE is set to RESTRICT, when it should be CASCADE.
I have to manually change the FK to get it work.

Edit : Bug filed on Mysql bug tracker.

Lanza
  • 1
  • 1