[英]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
实体框架 (EF) 代码优先级联删除,用于一对零或一关系
I have a normal context etc.我有一个正常的上下文等。
If i change anything, i can generate a new migration per Add-Migration test
.如果我更改任何内容,我可以根据
Add-Migration test
生成一个新的迁移。
But if i change WillCascadeOnDelete() from true to false or adding some with true it is ignored by entity framework.但是,如果我将 WillCascadeOnDelete() 从 true 更改为 false 或添加一些 true,则实体框架会忽略它。
I'm using Code first with a generated model from database.我首先将代码与数据库中生成的模型一起使用。 In the generated model everything was on
WillCascadeOnDelete(false)
.在生成的模型中,一切都在
WillCascadeOnDelete(false)
。 So now I'm changing it from false to true but its ignored by entity framework.所以现在我将它从 false 更改为 true 但它被实体框架忽略了。
I tried this: http://msdn.microsoft.com/en-us/data/jj591620.aspx#CascadeDelete too.我也试过这个: http : //msdn.microsoft.com/en-us/data/jj591620.aspx#CascadeDelete 。
After adding this lines ... Nothing changes if i add Add-Migration newTest
.添加此行后......如果我添加
Add-Migration newTest
则没有任何变化。
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>()
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>()
This is ignored, too, by Add-Migration thirdTest
.这也被
Add-Migration thirdTest
。
modelBuilder.Conventions.Add<OneToManyCascadeDeleteConvention>()
modelBuilder.Conventions.Add<ManyToManyCascadeDeleteConvention>()
I can change everything with WillCascadeOnDelete... It is ignored!我可以用 WillCascadeOnDelete 改变一切……它被忽略了!
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:我有这个表:
The SomeToThing
has additional variables and because that i can't map Some
directly to Thing
. SomeToThing
有额外的变量,因为我无法将Some
直接映射到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.在我第一次尝试时,我忘记设置
.WillCascadeOnDelete(false)
,并且出于充分的理由,SQL Server 由于周期而拒绝了迁移。 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.然后,当我在
OnModelCreating
方法中删除级联后尝试使用相同的名称重新构建迁移时,EF 只是不会选择那些特定的更改。
Then I deleted the migration source file and ran Add-Migration SameMigrationName
.然后我删除了迁移源文件并运行
Add-Migration SameMigrationName
。 Cascade deletes were removed, and seems like it worked, since MSSQL accepted the migration script.由于 MSSQL 接受了迁移脚本,级联删除已被删除,并且似乎有效。 I'm using EF 6.1.3 btw.
顺便说一句,我正在使用 EF 6.1.3。
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):关于我遇到的问题,我想提到的一件事是我添加了以下代码(到我的“OnModelCreating”,即 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.但是我在定义 MyEntityOne 的表已经定义之后添加了这个,即在以前的迁移中。
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.事实证明,因为我在表已经定义之后将它添加到 OnModelCreating 中,所以当我添加一个新的迁移时,实体框架并没有“不”选择 willcascadeondelete(false)。
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.通过添加AddForeignKey 行,使用cascadeDelete: false,它将重新应用带有cacade on delete false 的外键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.