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");
});
I saw this example only that here it does in Onmodelcreating and I am creating a separate Map class for each model class.
– Marco Souza
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.
– Ayrton Giffoni
for you to call your classes in onModelCreating: modelBuilder.Configurations.Add(new Employeesmap());
– Ayrton Giffoni
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.
– Marco Souza
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
– Ayrton Giffoni
I get it, thank you.
– Marco Souza