Entity Framework Data Annotations equivalent of .WillCascadeOnDelete(false);

Jim Wolff picture Jim Wolff · May 3, 2012 · Viewed 9.8k times · Source

I'm currently using EF Code First 4.3 with migrations enabled, but automatic migrations disabled.

My question is simple, is there a data annotations equivalent of the model configuration .WillCascadeOnDelete(false)

I would like to decorate my class so that the foreign key relationships do NOT trigger a cascading delete.

Code sample:

public class Container
{
    public int ContainerID { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Output> Outputs { get; set; }
}

public class Output
{
    public int ContainerID { get; set; }
    public virtual Container Container { get; set; }

    public int OutputTypeID { get; set; }
    public virtual OutputType OutputType { get; set; }

    public int Quantity { get; set; }
}  

public class OutputType 
{
    public int OutputTypeID { get; set; }
    public string Name { get; set; }
}

I Would like to do something like this:

public class Output
{
    [CascadeOnDelete(false)]   
    public int ContainerID { get; set; }
    public virtual Container Container { get; set; }

    [CascadeOnDelete(false)]    
    public int OutputTypeID { get; set; }
    public virtual OutputType OutputType { get; set; }

    public int Quantity { get; set; }
}  

This way i would be able to scaffold the migration correctly. which scaffolds the foreign key relationships to be cascade deleted at the moment.

Any ideas, other than using Model Configuration?

Answer

Ladislav Mrnka picture Ladislav Mrnka · May 3, 2012

No there is no such equivalent. You must use fluent API to remove cascade delete selectively or you must remove OneToManyCascadeDelete convention to remove it globally.