[英]Introducing FOREIGN KEY constraint 'FK_Jobs_AspNetUsers_UserId' on table 'Jobs' may cause cycles or multiple cascade paths
I have the following entities in my project, and I get an error when trying to update my database(SQL Server).我的项目中有以下实体,在尝试更新数据库(SQL Server)时出现错误。 I have looked around stack overflow, seen some people having the exact same problem as me, but when I try their fixes, I still get the same error.我查看了堆栈溢出,看到有些人遇到了与我完全相同的问题,但是当我尝试他们的修复时,我仍然得到同样的错误。 I have been tackling this error for a while now and I don't seem to know what exactly is wrong.我已经解决这个错误一段时间了,我似乎不知道到底是什么问题。 Please I think I might need some help in figuring out this one.请我想我可能需要一些帮助来解决这个问题。
My AppUser
entity:我的AppUser
实体:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Bio { get; set; }
public string City { get; set; }
public DateTime CreatedAt { get; set; }
public virtual ICollection<Job> CreatedJobs { get; set; }
public virtual ICollection<Job> AssignedJobs{ get; set; }
public string Avatar { get; set; }
public virtual ICollection<Watch> Watching { get; set; }
public string Tagline { get; set; }
public Socials Socials { get; set; }
public virtual ICollection<Skill> SkillSet { get; set; }
public virtual ICollection<Language> Languages { get; set; }
}
My Job
entity我的Job
实体
public class Job
{
public Guid Id { get; set; }
public virtual ICollection<Photo> Photos { get; set; }
public string Title { get; set; }
public string Description { get; set; }
[Column(TypeName = "decimal(18,2)")]
public decimal InitialPrice { get; set; }
public string UserId { get; set; }
public virtual ApplicationUser User { get; set; }
public string AssignedUserId { get; set; }
public virtual ApplicationUser AssignedUser { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime DeliveryDate { get; set; }
public virtual ICollection<Bid> Bids { get; set; }
public virtual ICollection<Watch> Watching { get; set; }
public string Address{ get; set; }
public DeliveryTypes DeliveryType { get; set; }
public string PostCode { get; set; }
public int Views { get; set; }
public Category Category { get; set; }
public JobStatus JobStatus { get; set; }
}
What I've done for my table configuration:我为我的表配置做了什么:
modelBuilder.Entity<ApplicationUser>()
.HasMany(c => c.CreatedJobs)
.WithOne(t => t.User)
.HasForeignKey(t => t.UserId)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<ApplicationUser>()
.HasMany(c => c.AssignedJobs)
.WithOne(t => t.AssignedUser)
.HasForeignKey(t => t.AssignedUserId)
.OnDelete(DeleteBehavior.NoAction);
I have tried all the DeleteBehavior
values and none seem to work.我已经尝试了所有DeleteBehavior
值,但似乎都没有。
I still get the error -我仍然得到错误 -
Microsoft.EntityFrameworkCore.Database.Command[20102] Failed executing DbCommand (28ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] ALTER TABLE [Jobs] ADD CONSTRAINT [FK_Jobs_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE; Microsoft.EntityFrameworkCore.Database.Command[20102] 未能执行 DbCommand (28ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] ALTER TABLE [Jobs] 添加约束 [FK_Jobs_AspNetUsers_UserId] FOREIGN KEY ([UserId] ) 参考 [AspNetUsers] ([Id]) 删除级联; Failed executing DbCommand (28ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] ALTER TABLE [Jobs] ADD CONSTRAINT [FK_Jobs_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE;执行 DbCommand 失败 (28ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] ALTER TABLE [Jobs] ADD CONSTRAINT [FK_Jobs_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id])删除级联; Microsoft.Data.SqlClient.SqlException (0x80131904): Introducing FOREIGN KEY constraint 'FK_Jobs_AspNetUsers_UserId' on table 'Jobs' may cause cycles or multiple ca scade paths. Microsoft.Data.SqlClient.SqlException (0x80131904):在表 'Jobs' 上引入 FOREIGN KEY 约束 'FK_Jobs_AspNetUsers_UserId' 可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。 Could not create constraint or index.无法创建约束或索引。 See previous errors.请参阅以前的错误。
I know this question has been asked before.我知道以前有人问过这个问题。 I have tried the fixes people suggested and it doesn't seem to work in my case, hence why I feel the need to ask again.我已经尝试了人们建议的修复程序,但它似乎对我的情况不起作用,因此我觉得有必要再次询问。
i think its migration versioning issue.我认为它的迁移版本问题。 you added a migration then you added the cascade delete thingy, so its still breaking on the first one,to achieve that你添加了一个迁移然后你添加了级联删除的东西,所以它仍然在第一个中断,以实现这一点
run Update-Database –TargetMigration: <name of last good migration>
运行Update-Database –TargetMigration: <name of last good migration>
then然后
run remove-migration name_of_bad_migration
运行remove-migration name_of_bad_migration
now add a new migration with the new change现在添加具有新更改的新迁移
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.