Entity Framework Core: How to solve Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths

后端 未结 4 918
清酒与你
清酒与你 2021-01-11 10:30

I\'m using Entity Framework Core with Code First approach but recieve following error when updating the database:

Introducing FOREIGN KEY constraint \

相关标签:
4条回答
  • 2021-01-11 10:59
    public Guid? UsuarioId { get; set; }
    
    builder.Entity<Orcamentacao>()
           .HasOne(x => x.Usuario)
           .WithMany(x => x.Orcamentacaos)
           .OnDelete(DeleteBehavior.Restrict)
           .IsRequired(false)
           .HasForeignKey(x => x.UsuarioId);
    
    0 讨论(0)
  • 2021-01-11 11:00

    This is what I did from the answer of Dmitry,

    and It worked for me.

    Class:

    public class EnviornmentControls
    {
        public int Id { get; set; }
        ...
    
        public virtual Environment Environment { get; set; }
    }
    

    And it's Mapping

    public EnviornmentControlsMap(EntityTypeBuilder<EnviornmentControls> entity)
    {
            entity.HasKey(m => m.Id);           
    
            entity.HasOne(m => m.Environment)
                .WithMany(m => m.EnviornmentControls)
                .HasForeignKey(m => m.EnvironmentID)
                .OnDelete(DeleteBehavior.Restrict); // added OnDelete to avoid sercular reference 
    }
    
    0 讨论(0)
  • 2021-01-11 11:04

    These solutions didn't work for my case, but I found a way. I am not quite sure yet if it is safe but there's just something that's happening with deleting. So I modified the generated Migration File instead of putting an override.

    onDelete: ReferentialAction.Cascade
    

    The reason I did this because all the overriding mentioned above is not working for me so I manually removed the code which relates to Cascading of Delete.

    Just check which specific relation being mentioned at the error so you can go straightly.

    Hope this will be able to help for some people who's having the same issue as mine.

    0 讨论(0)
  • 2021-01-11 11:06

    In your sample code in OnModelCreating you have declared modelBuilder.Entity<AnEventUser>().HasOne(e => e.User)... twice: at start of method and at end.

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<AnEventUser>()       // THIS IS FIRST
            .HasOne(u => u.User).WithMany(u => u.AnEventUsers).IsRequired().OnDelete(DeleteBehavior.Restrict);
    
    
        modelBuilder.Entity<AnEventUser>()
            .HasKey(t => new { t.AnEventId, t.UserId });
    
        modelBuilder.Entity<AnEventUser>()
            .HasOne(pt => pt.AnEvent)
            .WithMany(p => p.AnEventUsers)
            .HasForeignKey(pt => pt.AnEventId);
    
        modelBuilder.Entity<AnEventUser>()       // THIS IS SECOND.
            .HasOne(eu => eu.User)               // THIS LINES
            .WithMany(e => e.AnEventUsers)       //   SHOULD BE
            .HasForeignKey(eu => eu.UserId);     //   REMOVED
    
    }
    

    Second call overrides first. Remove it.

    0 讨论(0)
提交回复
热议问题