3

I'm trying to link up two models in my database ("EventComments" and "ApplicationUsers") but i keep getting this error

Error Number:1785,State:0,Class:16 Introducing FOREIGN KEY constraint 'FK_EventComments_AspNetUsers_UserId' on table 'EventComments' may cause cycles or multiple cascade 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.

Here are my models.

public class EventComment
{
    [Key]
    public int Id { get; set; }

    [Required]
    [StringLength(2000)]
    public string Message { get; set; }

    [Required]
    public int EventId { get; set; }

    public Event Event { get; set; }

    [Required]
    public string UserId { get; set; }

    public ApplicationUser User { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public ICollection<Event> Events { get; set; } = new HashSet<Event>();
    public ICollection<EventComment> EventComments { get; set; } = new HashSet<EventComment>();
}

Here is my Fluent API config

    protected override void OnModelCreating(ModelBuilder builder)
        {
            //user
            builder
                .Entity<ApplicationUser>()
                .HasMany(u => u.Events)
                .WithOne(b => b.User);

            builder
                .Entity<ApplicationUser>()
                .HasMany(u => u.EventComments)
                .WithOne(b => b.User);

            builder
                .Entity<Event>()
                .HasMany(e => e.Comments)
                .WithOne(c => c.Event);
}

Specifying On Delete actions won't help in any way. Any suggestions would be appreciated.

Nkosi
  • 235,767
  • 35
  • 427
  • 472

0 Answers0