I have the following entities in my project, and I get an error when trying to update my database(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:
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
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.
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; 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.Data.SqlClient.SqlException (0x80131904): Introducing FOREIGN KEY constraint 'FK_Jobs_AspNetUsers_UserId' on table 'Jobs' may cause cycles or multiple ca scade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. 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>
then
run remove-migration name_of_bad_migration
now add a new migration with the new change
User contributions licensed under CC BY-SA 3.0