Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cascade Delete Rule in EF 4.1 Code First when using Shared Primary Key Association

I implemented a bidirectional 1:1 relationship based on this answer:

Primary /Foreign Key in Entity Framework

I define the bidirectional relation this way:

public class Student
{   
    public virtual int StudentId { get; set; }
    public virtual Anamnesis Anamnesis { get; set; }

    . . .
}

public class Anamnesis
{
    [Key, ForeignKey("Student")]
    public int AnamnesisId { get; set; }

    public virtual Student Student { get; set; }

    . . .
}

where, Student is the principal entity and Anamnesis it the entity that shares the PK.

Now I'd like that the relationship created had a Delete Rule = CASCADE. Actually, the relationship that is being created has Delete Rule = NO ACTION as seen in the following picture:

enter image description here

If I manually delete this relation inside the Table Properties window and add other relation with Delete Rule = CASCADE, the code works as I expect allowing me to delete a Student and it's shared Anamnesis that has the same ID.

So, here goes my question:

Is there a way of using Data Annotation (not Fluent API) in my class so that I get a Relation with CASCADE delete rule? I'd prefer using Data Annotation but if it's not possible, I'd be happy with some Fluent API code that makes this work.

NOTE

I have tried the Fluent API code that is shown in this post. It doesn't work in my case where I have bidirectional properties.

like image 443
Leniel Maccaferri Avatar asked Apr 28 '11 22:04

Leniel Maccaferri


People also ask

When should I delete cascade?

Use the ON DELETE CASCADE option to specify whether you want rows deleted in a child table when corresponding rows are deleted in the parent table. If you do not specify cascading deletes, the default behavior of the database server prevents you from deleting data in a table if other tables reference it.

How do I enable cascade delete in Entity Framework?

Cascade delete automatically deletes dependent records or sets null to ForeignKey columns when the parent record is deleted in the database. Cascade delete is enabled by default in Entity Framework for all types of relationships such as one-to-one, one-to-many and many-to-many.

What is Cascade delete in EF core?

Cascade delete allows the deletion of a row to trigger the deletion of related rows automatically. EF Core covers a closely related concept and implements several different delete behaviors and allows for the configuration of the delete behaviors of individual relationships.


2 Answers

The following fluent API code perfectly switch on the cascade delete on the database:

public class Student
{   
    public virtual int StudentId { get; set; }
    public virtual Anamnesis Anamnesis { get; set; }
}

public class Anamnesis
{        
    public int AnamnesisId { get; set; }
    public virtual Student Student { get; set; }
}

public class Context : DbContext
{
    public DbSet<Student> Students { get; set; }
    public DbSet<Anamnesis> Anamnesises { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Student>()
                    .HasRequired(s => s.Anamnesis)
                    .WithRequiredPrincipal(a => a.Student)
                    .WillCascadeOnDelete();
    }
}

enter image description here

like image 200
Morteza Manavi Avatar answered Nov 13 '22 07:11

Morteza Manavi


Also, you can use [Required] Attribute, and it will automatically set the delete rule to "CASCADE" mode in related relationship. (and also set "Allow Null" property of that entity to "false" in DB)

like image 23
RainClick Avatar answered Nov 13 '22 07:11

RainClick