none
Entity Framework CTP5, code-first. Many to many with cascade delete.

    Question

  •  

    I have two entities (Customer and CustomerRole) and would like to declare many-to-many relationship between them. I can do using the following code: 

    modelBuilder.Entity<CustomerRole>()
            .HasMany(cr => cr.Customers) 
            .WithMany(c => c.CustomerRoles)
            .Map(m => m.ToTable("Customer_CustomerRole_Mapping"));

    But it creates the relationship (and the third mapping table) with cascade delete switched off by default. How can I tell EF to create the relationship with cascade delete switched on when using many-to-many?


     

    • Edited by and.maz Tuesday, January 04, 2011 12:31 PM typo
    • Moved by Jackie-Sun Wednesday, January 05, 2011 2:12 AM (From:ADO.NET Entity Framework and LINQ to Entities)
    Tuesday, January 04, 2011 12:30 PM

All replies

  • Hello And.maz,

    Welcome to the MSDN Forum and thank you for posting here.

    In my opinion, your question is a EF pre-release related case, so I will move it to the right forum to make sure you can get a better support. 

    Thank you for your understanding and supporting!

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 05, 2011 2:11 AM
  • Hello *,

    Since the link table is nothing but a many-to-many association if you load the related navigation property, Ef would ensure that it issues deletes to the link table as well. For instance this should work for u.

     

    var drink = db.Categories.Include(c => c.Products) .FirstOrDefault(c => c.Name == "Drinks");

                    db.Categories.Remove(drink);

                    db.SaveChanges();//deletes will be issued to CategoryProducts table as well.

    The reason above code issues a deletes to link table is because when you mark a category as deleted, EF tries to find all the related entities loaded in the context and also marks them as deleted which results in issuing deletes to link table as well. (deletes ones that cannot exist without parent like identifying relationship)

    As far as i recall you cannot create cascade rule at the conceptual level for many-to-many association(don't know why) but if the cascade rule is defined at the database layer then this error could be bypassed.  I tried several places i didn't find cascade option either.

     


    Zeeshan Hirani Entity Framework 4.0 Recipes by Apress
    http://weblogs.asp.net/zeeshanhirani
    Wednesday, January 05, 2011 6:22 AM
  • And.maz,

     

    In addition to Zeeshan’s explanation there is also a discussion about the same issue in this thread: http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/06148576-9507-49ed-a859-1516f6b6af64

     

    Thanks,

    Arthur

    Wednesday, January 05, 2011 11:04 PM
    Moderator