0

I have the following classes and EF convention created the tables fine - all good. Now I want to configure cascade delete on the ToDoList (so that when I delete a ToDoList, all the ToDoItems and UserToDoLists also gets deleted automatically). Must I re-specify what EF convention is able to detect in order to set WillCascadeOnDelete? How do I specify this relationship in fluent API?

public class ToDoList
{
    public int Id { get; set; }
    public string Description { get; set; }
    public virtual ICollection<ToDoItem> ToDoItems { get; set; }
    public virtual ICollection<UserToDoList> UserToDoLists { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public ApplicationUser()
        : base()
    {
    }

    public virtual ICollection<UserToDoList> UserToDoLists { get; set; }
}

// a todolist can have many users and a user can have many todolist
// junction table will have addition fields
public class UserToDoList
{
    public string UserId { get; set; }
    public virtual ApplicationUser User { get; set; }

    public virtual int ToDoListId { get; set; }
    public virtual ToDoList ToDoList { get; set; }

    // additional fields
    public bool IsOwner { get; set; }
    public int Ordinal { get; set; }
    public bool AllowEdit { get; set; }
}

public class UserToDoListConfig : EntityTypeConfiguration<UserToDoList>
{
    public UserToDoListConfig()
    {
        HasKey(ut => new { ut.UserId, ut.ToDoListId });
    }
}

public class ToDoListConfig : EntityTypeConfiguration<ToDoList>
{
    public ToDoListConfig()
    {
        Property(t => t.Description).IsRequired().HasMaxLength(50);

        HasMany(t =>t.UserToDoLists).??
    }
}
lee23
  • 409
  • 1
  • 3
  • 10
  • Possible duplicate of [Entity Framework (EF) Code First Cascade Delete for One-to-Zero-or-One relationship](http://stackoverflow.com/questions/17487577/entity-framework-ef-code-first-cascade-delete-for-one-to-zero-or-one-relations) – Kahbazi Aug 02 '16 at 09:45
  • Cascade delete should be the default behavior for `one-to-many` relationships like yours. Did you check the generated tables/foreign keys? – Ivan Stoev Aug 02 '16 at 10:14
  • mine is a many to many relationship – lee23 Aug 02 '16 at 12:44
  • @lee23 You are thinking for `Application` - `ToDoList` which is indeed `many-to-many`. But you implemented it with explicit `UserToDoList` junction table and 2 `one-to-many` relationships `User` - `UserToDoList` and `ToDoList` - `UserToDoList`. And again, these relations by default has cascade delete on. – Ivan Stoev Aug 05 '16 at 19:43

1 Answers1

0

That's a simple one-to-many relationship from ToDoList to your junction table UserToDoList, so you just have to set up the mapping like this

HasMany(m => m.UserToDoLists)
    .WithRequired(m => m.ToDoList)
    .HasForeignKey(m => m.ToDoListId);

and EF will handle your cascading deletes automatically, because ToDoList is required on the junction table side.

Florian Haider
  • 1,892
  • 18
  • 23
  • Thank you, the problem was also me making the mistake of deleting from ApplicationUser.UserToDoLists rather than DbContext.ToDoLists. – lee23 Aug 03 '16 at 01:50