I have problem because when I add the following to class Course I have only 2 tables not 3
public int PersonId { get; set; }
[ForeignKey("PersonId")]
public virtual Person Student { get; set; }
you do not have these three lines all good, but I need an additional field in class Course
public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Course> CoursesAttending { get; set; }
}
public class Course
{
public int CourseId { get; set; }
public string Title { get; set; }
public int PersonId { get; set; }
[ForeignKey("PersonId")]
public virtual Person Student { get; set; }
public virtual ICollection<Person> Students { get; set; }
}
public class SchoolContext : DbContext
{
public DbSet<Course> Courses { get; set; }
public DbSet<Person> People { get; set; }
}
class Program
{
static void Main(string[] args)
{
Database.SetInitializer<SchoolContext>(
new DropCreateDatabaseAlways<SchoolContext>());
SchoolContext db = new SchoolContext();
var cos = from d in db.Courses
select d;
}
}
please help me
EF cannot decide if
Course.StudentorCourse.Studentsrefers toPerson.CoursesAttending. You must give EF a hint, for example by using the[InverseProperty]attribute onCourse.Students:Edit
The model will cause multiple cascading delete paths, namely: When you delete a
Personrecords in the join table will be deleted as well, but it will also delete allCourses that this person is assigned to through theCourse.Personproperty. The deletedCourses will delete records in the join table again which is the second delete path on the same table.Multiple cascading delete paths are not allowed with SQL Server, so you must disable it with Fluent API: