Many to Many with Fluent API

Asked

Viewed 343 times

3

I’m doing a mapping with Fluent API and I came up with a question on how I should do the mapping very much.

For example, I have the classes below.

public partial class Territories
{
    public Territories()
    {
        this.Employees = new HashSet<Employees>();
    }

    public int TerritoryID { get; set; }
    public string TerritoryDescription { get; set; }
    public int RegionID { get; set; }

    public virtual Region Region { get; set; }
    public virtual ICollection<Employees> Employees { get; set; }
}

and

public partial class Employees
{
    public Employees()
    {
        this.Employees1 = new HashSet<Employees>();
        this.Orders = new HashSet<Orders>();
        this.Territories = new HashSet<Territories>();
    }

    public int EmployeeID { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string Title { get; set; }
    public string TitleOfCourtesy { get; set; }
    public DateTime? BirthDate { get; set; }
    public DateTime? HireDate { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string Region { get; set; }
    public string PostalCode { get; set; }
    public string Country { get; set; }
    public string HomePhone { get; set; }
    public string Extension { get; set; }
    public byte[] Photo { get; set; }
    public string Notes { get; set; }
    public int? ReportsTo { get; set; }
    public string PhotoPath { get; set; }

    public virtual ICollection<Employees> Employees1 { get; set; }
    public virtual Employees Employees2 { get; set; }
    public virtual ICollection<Orders> Orders { get; set; }
    public virtual ICollection<Territories> Territories { get; set; }
}

In my class EmployeesMap created the mapping.

HasMany(p => p.Territories)
.WithMany(p => p.Employees)
.Map(m =>
{
  m.ToTable("EmployeeTerritories");
  m.MapLeftKey("EmployeeID");
  m.MapRightKey("TerritoryID");
});

My doubt here is in my class TerritoriesMap should I do the relationship again or not? ie do.

HasMany(p => p.Employees)
.WithMany(p => p.Territories)
.Map(m =>
{
  m.ToTable("EmployeeTerritories");
  m.MapLeftKey("EmployeeID");
  m.MapRightKey("TerritoryID");
});

2 answers

3


You decide, it is not necessary to do this to work. Mapping only on one side already makes everything work as it should.

Remember that having browsing properties on both sides of the relationship causes a circular dependency and you will have to deal with this in case of data serialization.

3

As we can see in the example here, you need to configure only one side.

Example:

public class Student
{
    public Student() 
    {
        this.Courses = new HashSet<Course>();
    }

    public int StudentId { get; set; }
    [Required]
    public string StudentName { get; set; }

    public virtual ICollection<Course> Courses { get; set; }
}

public class Course
{
    public Course()
    {
        this.Students = new HashSet<Student>();
    }

    public int CourseId { get; set; }
    public string CourseName { get; set; }

    public virtual ICollection<Student> Students { get; set; }
}

public class SchoolDBContext : DBContext
{
    public SchoolDBContext() : base("SchoolDB-DataAnnotations")
    {
    }

    public DbSet<Student> Students { get; set; }

    public DbSet<Course> Courses { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

    modelBuilder.Entity<Student>()
                .HasMany<Course>(s => s.Courses)
                .WithMany(c => c.Students)
                .Map(cs =>
                        {
                            cs.MapLeftKey("StudentRefId");
                            cs.MapRightKey("CourseRefId");
                            cs.ToTable("StudentCourse");
                        });

}

Your bank:

banco

  • 1

    I saw this example only that here it does in Onmodelcreating and I am creating a separate Map class for each model class.

  • 1

    Actually, there’s no difference, you’re just separating the responsibilities. In the end, you will have to call your class inside the Onmodelcreating anyway.

  • for you to call your classes in onModelCreating: modelBuilder.Configurations.Add(new Employeesmap());

  • 1

    That I did, but that’s when the doubt hit, when I separated the repesability I was in doubt in which class I should have the configuration and if I had to have in both.

  • 1

    So, but actually when you do it in the separate class and put it into the Onmodelcreating, it will act the same way as in the example I gave above. Then you just do the 1 side configuration only. This goes for 1x1 and 1xN as well

  • 1

    I get it, thank you.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.