Question

As far as i know, i have two way to implement many-to-many relation in asp.net mvc using code-first.

1- Fluent Api

public class HrPerson
{
  public int Id { get; set; }
  public string Name { get; set; }
  public virtual ICollection<HrPersonTitle> HrPersonTitle { get; set; }
}

public class HrPersonTitle
{
  public int Id { get; set; }
  public string Title { get; set; }
  public virtual ICollection<HrPerson> HrPerson { get; set; }
}


protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
  modelBuilder.Entity<HrPerson>()
  .HasMany(s => s.HrPersonTitle)
  .WithMany(c => c.HrPerson)
  .Map(t =>
     {
        t.MapLeftKey("HrPersonId")
         .MapRightKey("HrPersonTitleId")
         .ToTable("HrMapPersonTitle");
     });
}

2-Custom Mapping Table

    public class HrPerson
    {
      public int Id { get; set; }
      public string Name { get; set; }
      public virtual ICollection<HrMapPersonTitle> HrMapPersonTitle { get; set; }
    }

    public class HrPersonTitle
    {
      public int Id { get; set; }
      public string Title { get; set; }
      public virtual ICollection<HrMapPersonTitle> HrMapPersonTitle { get; set; }
    }

   public class HrMapPersonTitle
    {
        public int Id { get; set; }
        public int HrPersonId { get; set; }
        public int HrPersonTitleId { get; set; }
        public virtual HrPerson HrPerson { get; set; }
        public virtual HrPersonTitle HrPersonTitle { get; set; }
        public string Note { get; set; }
        public bool Deleted { get; set; }
    }

My questions:

If i choose second way, i am not able to reach HrPersonTitle.Name property from HrPerson model in the view. How can i reach the properties ?

If i choose the first way i can reach the HrPersonTitle.Name but i am not able to add more property in the map file ? How can i add more properties?

Regards.

Was it helpful?

Solution

When you create a M2M without a payload (just the foreign key relationships, no extra data), EF collapses the relationship so that you can query directly without having to explicitly go through the join table. However, if you need a payload, then EF can no longer manage the relationship in this way.

So, if you want to get the title, you have to go through HrMapPersonTitle:

@foreach (var title in Model.HrMapPersonTitle)
{
    @title.HrPersonTitle.Name
}

OTHER TIPS

Both these methods seem overkill maybe. I don't know your full intentions however I implement this all the time at work and I use the following:

public class HrPerson
{
  public int Id { get; set; }
  public string Name { get; set; }
  public virtual ICollection<HrPersonTitle> HrPersonTitles { get; set; }
}

public class HrPersonTitle
{
  public int Id { get; set; }
  public string Title { get; set; }
  public virtual ICollection<HrPerson> HrPersons { get; set; }
}


protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
  modelBuilder.Entity<HrPerson>()
  .HasMany(s => s.HrPersonTitles)
  .WithMany(c => c.HrPersons);
}

If you are using code first and you try and access either mapping within the DbContext it should Lazy Load your information and every property should be accessible.

I do have one question though. Are you sure it should be many to many, do they really have multiple titles?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top