0

I have three entities:

ApplicationUser:

public class ApplicationUser : IdentityUser<Guid>
{
    // more properties
    public List<MeetingNotification> MeetingNotifications { get; set; }
}

MeetingNotifications:

public class MeetingNotification
{
    public Guid Id { get; set; }
    public Guid MeetingId { get; set; }
    public Meeting Meeting { get; set; }
    public Guid SummonedUserId { get; set; }
    public ApplicationUser SummonedUser { get; set; }
}

Meetings:

public class Meeting
{
    public Guid Id { get; set; }
    // more properties
    public Guid OrganizerId { get; set; }
    public ApplicationUser Organizer { get; set; }
    public List<MeetingNotification> Notifications { get; set; }
}

The relationships are as such:

  • One ApplicationUser has many MeetingNotifications
  • One MeetingNotification has one ApplicationUser and one Meeting
  • One Meeting has many MeetingNotifications

When I try to update-database, I get the error message

Introducing FOREIGN KEY constraint 'FK_MeetingNotifications_Meetings_MeetingId' on table 'MeetingNotifications' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

This error is my arch nemesis, as I struggle to understand were the error lies. Is it in Meeting or in MeetingNotification. Or somewhere else?

I have tried these three definitions:

modelBuilder.Entity<MeetingNotification>()
    .HasOne(m => m.Meeting)
    .WithMany(n => n.Notifications)
    .HasForeignKey(fk => fk.Id).OnDelete(DeleteBehavior.NoAction);

modelBuilder.Entity<MeetingNotification>()
    .HasOne(m => m.Meeting)
    .WithMany(n => n.Notifications)
    .HasForeignKey(fk => fk.MeetingId).OnDelete(DeleteBehavior.NoAction);

modelBuilder.Entity<Meeting>()
    .HasMany(n => n.Notifications)
    .WithOne(m => m.Meeting)
    .HasForeignKey(fk => fk.MeetingId).OnDelete(DeleteBehavior.NoAction);

... but they don't seem to do anything. It's just the same error message every time

Update

I finally found a way!

modelBuilder.Entity<MeetingNotification>()
    .HasOne(m => m.Meeting)
    .WithMany(n => n.Notifications)
    .OnDelete(DeleteBehavior.Restrict);

... and the error went away! But I don't fully understand why, so any good explanation would be appreciated!

Stian
  • 1,522
  • 2
  • 22
  • 52
  • To my observation, I think you should use a composite key on MeetingNotification instead of a primary key `modelBuilder.Entity().HasKey(x => new { x.MeetingId, x.SummonedUserId })` – Tavershima Aug 04 '20 at 22:11
  • It seems that your `MeetingNotification` is a join table that creates a many-to-many relationship between the `ApplicationUser` and `Meeting`. Is that what you are trying to do? – Ankit Aug 05 '20 at 02:37
  • @Ankit That is correct. – Stian Aug 05 '20 at 04:14
  • @Tavershima That didn't make any difference. The same error message persists. – Stian Aug 05 '20 at 04:17
  • Could the error have something to do with `Meeting.Organizer` (`ApplicationUser`)? – Stian Aug 05 '20 at 04:25
  • What database are you using? I tried a minimal solution based on your classes using EFCore and Sqlite, and I did not get any error. My database-update was successful. Do you know if you get the same error with Sqlite? The only difference in my schema is that the ApplicationUser does not extend the IdentityUser class – Ankit Aug 05 '20 at 04:39
  • 1
    @Ankit I'm using SQLite as well. I have found a working solution, though. See updated question. I'm not sure why that worked and the other variations I tried previously didn't... – Stian Aug 05 '20 at 04:52

0 Answers0