简体   繁体   中英

How do I represent a one to many relationship with the Entity Framework?

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.

You can't define 1:N relationship between the entity and a string. In the EF it's only possible to have a relationship between two entities, so DialInNumber should be represented by an entity.

public class DialInNumber {
   public int ID { get; set; }
   public string Number { get; set; }
   public virtual Conference Conference { get; set;}
}

And Conference class should reference a collection of these entities instead of the collection of strings.

public class Conference {
    public string Title { get; set; }
    public string OwnerName { get; set; }
    public virtual ICollection<DialInNumber> DialInNumbers { get; set; }    
}

And a note: in order to make lazy-loading work, the DialInNumbers property should be virtual

public class Conference
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string OwnerName { get; set; }
    public List<string> DialInNumbers { get; set; }
}

Your entity has only scalar properties. This mean that it doesn't refer to another entity. In your specific case, the property DialInNumbers is a list of string which is not a list of entity. Entity Framework do navigation property into table relationship which is not the case now.

If you do want to have a table of DialInNumbers, you will need to have a list of your DialNumber.

public class Conference
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string OwnerName { get; set; }
    public List<DialNumber> DialInNumbers { get; set; }
}

public Class DialNumber
{
    public string Number{get;set;}
    public int ConferenceId{get;set;}
    public Conference Conference{get;set;}
}

You need to configure your entity for the navigation property in your configuration class.

modelBuilder
   .Entity<DialNumber>()
   .HasRequired(p => p.Conference);

If you want to use entity with the id you will need to modify you DialNumber to :

public Class DialNumber
{
    public string Number{get;set;}
    public Conference Conference{get;set;}
}

modelBuilder
   .Entity<DialNumber>()
   .HasRequired(p => p.Conference);
   .WithMany(b => b.DialNumber)
   .HasForeignKey(p => p.ConferenceId);

This will allow you to have less overhead of attaching entity later on and will allow you to have your foreign key named the way you want, in this case ConferenceId instead of the default syntax which would have produce (like in the first case) "Conference_Id".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM