Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 929
Jim Wolff Avatar asked May 03 '12 13:05

Jim Wolff


1 Answers

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.

like image 190
Ladislav Mrnka Avatar answered Sep 19 '22 19:09

Ladislav Mrnka