Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity framework migrations ignoring WillCascadeOnDelete

look at this question for example. Entity Framework (EF) Code First Cascade Delete for One-to-Zero-or-One relationship

I have a normal context etc.

If i change anything, i can generate a new migration per Add-Migration test.

But if i change WillCascadeOnDelete() from true to false or adding some with true it is ignored by entity framework.

I'm using Code first with a generated model from database. In the generated model everything was on WillCascadeOnDelete(false). So now I'm changing it from false to true but its ignored by entity framework.

I tried this: http://msdn.microsoft.com/en-us/data/jj591620.aspx#CascadeDelete too.

After adding this lines ... Nothing changes if i add Add-Migration newTest.

modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>()
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>()

This is ignored, too, by Add-Migration thirdTest.

modelBuilder.Conventions.Add<OneToManyCascadeDeleteConvention>()
modelBuilder.Conventions.Add<ManyToManyCascadeDeleteConvention>()

I can change everything with WillCascadeOnDelete... It is ignored!

If i change everything else, it works and would be appended in new Migrations...

The main class for this construct is the following.

[Table("SomeToThing")]
public class SomeToThing : Base
{
    [Column("Some")]
    public Guid SomeId { get; set; }
    [ForeignKey("SomeId")]
    public virtual Some Some { get; set; }

    [Column("Thing")]
    public Guid ThingId { get; set; }
    [ForeignKey("ThingId")]
    public virtual Thing Thing { get; set; }
}

I have this tables:

  • Some
  • SomeToThing
  • Thing

The SomeToThing has additional variables and because that i can't map Some directly to Thing.

like image 884
PatrickB Avatar asked Nov 10 '22 21:11

PatrickB


1 Answers

I know this thread is old, but I was just having the same issue.

My solution was to delete the migration source file and re-scaffolding it from scratch.

On my first try I forgot to set .WillCascadeOnDelete(false), and for good reasons, SQL Server rejected the migration due to cycles. Then when I tried to re-scaffold the migration using the same name after removing cascades in the OnModelCreating method, EF just wouldn't pick up those particular changes.

Then I deleted the migration source file and ran Add-Migration SameMigrationName. Cascade deletes were removed, and seems like it worked, since MSSQL accepted the migration script. I'm using EF 6.1.3 btw.

like image 120
Ricardo Pieper Avatar answered Nov 14 '22 21:11

Ricardo Pieper