简体   繁体   中英

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 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM