Migrating an Entity framework that ignores WillCascadeOnDelete

look at this question for example. Entity Framework (EF) code The first cascade search for a one-on-one or one relationship

I have a normal context, etc.

If I change something, I can create a new migration in the Add-Migration test .

But if I change the value of WillCascadeOnDelete () from true to false or adding some with true, it is ignored by the entity framework.

I use Code first with the model created from the database. In the generated model, everything was on WillCascadeOnDelete(false) . So, now I change it from false to true, but the entity structure ignores it.

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

After adding these lines ... Nothing will change if I add Add-Migration newTest .

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

This is also ignored 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 will work and be added to the new Migrations ...

The main class for this construction is as follows.

 [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 the following tables:

  • Some
  • SomeToThing
  • Thing

SomeToThing has additional variables because I cannot match Some directly with Thing .

+5
source share
1 answer

I know this thread is outdated, but I had the same problem.

My solution was to delete the original migration file and restore it from scratch.

On my first attempt, I forgot to install .WillCascadeOnDelete(false) , and for good reason, SQL Server rejected the migration due to loops. Then, when I tried to redo the migration using the same name after removing the cascades in the OnModelCreating method, EF simply could not get these specific changes.

Then I deleted the original migration file and ran Add-Migration SameMigrationName . The Cascade removal has been removed and it looks like it worked, since MSSQL accepted the migration script. I am using EF 6.1.3 btw.

+1
source

Source: https://habr.com/ru/post/1209120/


All Articles