5

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.

Community
  • 1
  • 1
PatrickB
  • 3,225
  • 5
  • 31
  • 55
  • You can always write your own migrations – jao Dec 15 '14 at 15:51
  • Yes thats what I tried after Add-Migration not working... I think that was my fail and not the fail from Add-Migration Tool ... Because that I asked the community :) – PatrickB Dec 16 '14 at 07:54
  • Migrations doesn't do everything. If I remember correctly, it doesn't for example add unique indexes as well. – jao Dec 16 '14 at 08:10
  • Have you an example migration function/string which alters an foreign key to CascadeDelete: true?? Thanks in Advance! – PatrickB Dec 16 '14 at 09:10
  • I'm having this issue too, it seems that when you have some kind of inheritance and you try to configure the derived entity, entity framework ignores WillCascadeOnDelete for no reason. My scenario is: I don't want cascade deleting on every entity except for one. – Ariel Moraes Nov 30 '15 at 19:22

2 Answers2

1

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.

Ricardo Pieper
  • 2,613
  • 3
  • 26
  • 40
0

One thing I would like to mention, relating to the problem I was having, was that I added the following code (to my 'OnModelCreating', ie DbContext):

 modelBuilder.Entity<MyEntityTwo>()
                .HasMany(e => e.MyEntityOne)
                .WithRequired(e => e.MyEntityTwo)
                .HasForeignKey(e => e.MyEntityTwoId)
                .WillCascadeOnDelete(false); 

but I added this AFTER the table defining MyEntityOne was already defined, ie in a previous migration.

Turns out that since I added this to OnModelCreating after the table was already defined, entity framework did 'not' pickup the willcascadeondelete(false) when I added a new migration.

To fix this, I had to manually add the following to (new) migration:

DropIndex(...this was created by EF for me already.  Not important...);
DropForeignKey("MyEntityOne", "<name of Foreign key goes here ie FK_MyEntityOne_...etc>");
AddForeignKey("MyEntityOne", "MyEntityTwoId", "MyEntityTwo", "Id", cascadeDelete:false);
AlterColumn(...this was created by EF for me already.  Not important...);
CreateIndex(...this was created by EF for me already.  Not important...);

By adding the AddForeignKey line, with cascadeDelete: false, it will re-apply the foreign key with cacade on delete false this time.

Jeff Moretti
  • 613
  • 5
  • 6