This was working and now it's not. Only difference, my model had so many changes I deleted all the migrations and started over having it build the DB from scratch. I thought when there's a many to many relationship in Entity Framework that deleting one of the objects did not delete the many objects it's linked to.
That's what I want. When a User or Campaign is deleted, I do not want the many objects it is linked to deleted.
public class User
{
public int Id { get; private set; }
public ICollection<Campaign> Campaigns { get; set; }
}
public class Campaign
{
public int Id { get; private set; }
public ICollection<User> Followers { get; set; }
}
And the errors I now get running Update-Database are:
Failed executing DbCommand (8ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [CampaignUser] (
[CampaignsId] int NOT NULL,
[FollowersId] int NOT NULL,
CONSTRAINT [PK_CampaignUser] PRIMARY KEY ([CampaignsId], [FollowersId]),
CONSTRAINT [FK_CampaignUser_Campaigns_CampaignsId] FOREIGN KEY ([CampaignsId]) REFERENCES [Campaigns] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_CampaignUser_Users_FollowersId] FOREIGN KEY ([FollowersId]) REFERENCES [Users] ([Id]) ON DELETE CASCADE
);
and
Introducing FOREIGN KEY constraint 'FK_CampaignUser_Users_FollowersId' on table 'CampaignUser' 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.
Any idea why this is happening? I do want the join table entry to go away when a Campaign or User object is deleted. But I do not want the associated ICollection of objects to be deleted.
I tried fluent commands to explicitly set this, but every command I tried wouldn't compile. So I'm clearly not understanding this at all.
public class CampaignConfiguration : IEntityTypeConfiguration<Campaign>
{
/// <inheritdoc />
public void Configure(EntityTypeBuilder<Campaign> builder)
{
builder.HasMany(c => c.Followers)
.WithMany(u => u.Campaigns).OnDelete()
}
}
thanks - dave