I implemented a bidirectional 1:1 relationship based on this answer:
Primary /Foreign Key in Entity Framework
I define the bidirectional relation this way:
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)
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();
}
}