Entity Framework Core foreign keys many

asp.net c# entity-framework-core

Question

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.

1
0
3/22/2018 8:18:54 AM

Popular Answer

You can do it by fluent api

modelBuilder.Entity<Seminars>()
.HasMany(c => c.Students)
.WithOptional()
.Map(m => m.MapKey("StudentId"));
0
3/22/2018 10:32:46 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow