I'm using Entity Framework Core in context of a ASP.Net Core MVC application. A snippet of the data model looks like that:
public class Seminar
{
public int ID { get; set; }
public string Name { get; set; }
public Person Teacher { get; set; }
public int TeacherID { get; set; }
public IList<Person> Students { get; set; }
public Seminar()
{
Students = new List<Person>();
}
}
EF automatically assigns the property TeacherID
to reflect the ID of the entity Teacher
(foreign key). This is quite handy to be used in ASP.Net. Is there any similar concept for the to-many-reference Students
?
At the end I would like to create a multi-select in ASP.Net. Hence, I need a list of IDs of assigned Students
to this seminar.
You can do it by fluent api
modelBuilder.Entity<Seminars>()
.HasMany(c => c.Students)
.WithOptional()
.Map(m => m.MapKey("StudentId"));