How do I represent a one to many relationship in ASP.NET MVC with the Entity Framework?
My project is a conference calling project. I have a database of Conferences
. Each virtual Conference
room has a Title
, an OwnerName
, and a list of different DialInNumbers
. I know that in order to make this relationship of one Conference
to many DialInNumbers
, I'll need at least two tables: one for the Conferences
, and one for the DialInNumbers
at least. I've been having problems creating, modifying and accessing the DialInNumbers
. My code is definitely missing something. Let's take a look:
Here is the model for the Conference
:
public class Conference
{
public int Id { get; set; }
public string Title { get; set; }
public string OwnerName { get; set; }
public List<string> DialInNumbers { get; set; }
}
The model requires a DbContext
(not really sure why...)
public class ConferenceDbContext : DbContext
{
public DbSet<Conference> Conferences { get; set; }
}
Then, in the Controller
(ConferenceController
), we can Create
and Edit
a Conference
. For example:
public ActionResult Create(Conference conference)
{
if (ModelState.IsValid)
{
conference.DialInNumbers = ThreeRandomlyGeneratedPhoneNumbers();
db.Conferences.Add(conference);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(conference);
}
When I save the database changes, I only get one table for the Conferences
. How do I get multiple tables and how do I link them together in the Entity Framework?
Thank you.