Consider a Course can has a Curriculum or not. So the relation between the Course and Curriculum is 1..0 or 1..1. The classes are:
public class Course
{
public int Id {get; set;}
public Curriculum { get; set; }
}
public class Curriculum
{
[ForeignKey("Course")]
public int Id { get; set; }
public Course Course { get; set; }
}
I configured the relation by fluent api like below:
public class CourseConfiguration : EntityTypeConfiguration<Course>
{
public CourseConfiguration()
{
HasOptional(course => course.Curriculum)
.WithRequired(cur => cur.Course);
}
}
unfortunately, when i want to save a course in the Db this error is raised:
Cannot insert explicit value for identity column in table 'Curricula' when IDENTITY_INSERT is set to OFF
Additionally, i added attributes [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
or [DatabaseGenerated(DatabaseGeneratedOption.None)]
to the primary key of Curriculum class, but it has no effect or creates new exception!
Do you have any idea?