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
WillCascadeOnDelete(true). What's the question? - Gert Arnold