I have a problem when I try to add constraints to my tables. I get the error:
Introducing FOREIGN KEY constraint \'FK74988DB24B3C886\' on table \'Empl
This is because Emplyee might have Collection of other entity say Qualifications and Qualification might have some other collection Universities e.g.
public class Employee{
public virtual ICollection Qualifications {get;set;}
}
public class Qualification{
public Employee Employee {get;set;}
public virtual ICollection Universities {get;set;}
}
public class University{
public Qualification Qualification {get;set;}
}
On DataContext it could be like below
protected override void OnModelCreating(DbModelBuilder modelBuilder){
modelBuilder.Entity().HasRequired(x=> x.Employee).WithMany(e => e.Qualifications);
modelBuilder.Entity.HasRequired(x => x.Qualification).WithMany(e => e.Universities);
}
in this case there is chain from Employee to Qualification and From Qualification to Universities. So it was throwing same exception to me.
It worked for me when I changed
modelBuilder.Entity().**HasRequired**(x=> x.Employee).WithMany(e => e.Qualifications);
To
modelBuilder.Entity().**HasOptional**(x=> x.Employee).WithMany(e => e.Qualifications);