0
votes

I'm trying to make a one to many self reference using EF6 code first. My entity looks like this

public class Menu
{
    public int Id { get; set; }
    public bool Target { get; set; }
    public string Url { get; set; }   
    [DefaultValue(null)]
    public int? ParentMenuId { get; set; }
    public virtual Menu ParentMenu { get; set; }
    public virtual List<Menu> ChildMenus { get; set; }
}  

And in my context class I do this to make the self reference with WillCascadeOnDelete(true)

            modelBuilder.Entity<Menu>()
            .HasOptional(c => c.ParentMenu)
            .WithMany(c => c.ChildMenus)
            .HasForeignKey(c => c.ParentMenuId)
            .WillCascadeOnDelete(true);

But when i update database in package manager console it's give an error

Introducing FOREIGN KEY constraint 'FK_dbo.Menus_dbo.Menus_ParentMenuId' on table 'Menus' 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. enter image description here

1
You have WillCascadeOnDelete(true). What's the question? - Gert Arnold
I recive error when i am update database in package manager console - Karen Grigoryan
You're still not asking anything, only telling a little story. You want cascaded delete, SQL server tells you you can't and even explains why not. Really, I'm not trying to nag you but I have no clue what you're asking. - Gert Arnold
My question is that - why i receive error in package manager console - Karen Grigoryan
What i should change in my model - Karen Grigoryan

1 Answers

1
votes

Finally i found that there is no any way in code first self reference entity Cascade Delete ,i ought to write recursive function for cascade deleting