sql server ce - Cascade Delete Rule in EF 4.1 Code First when using Shared Primary Key Association -


i implemented bidirectional 1:1 relationship based on answer:

primary /foreign key in entity framework

i define bidirectional relation 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 principal entity , anamnesis entity shares pk.

now i'd relationship created had delete rule = cascade. actually, relationship being created has delete rule = no action seen in following picture:

enter image description here

if manually delete relation inside table properties window , add other relation delete rule = cascade, code works expect allowing me delete student , it's shared anamnesis has same id.

so, here goes question:

is there way of using data annotation (not fluent api) in class relation cascade delete rule? i'd prefer using data annotation if it's not possible, i'd happy fluent api code makes work.

note

i have tried fluent api code shown in post. doesn't work in case have bidirectional properties.

the following fluent api code switch on cascade delete on 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


Comments