0

I am in the process of implementing an entity framework and code-first technique using Fluent API.

I already have a mapping for one table:

modelBuilder.Entity<Certificate>()
            .HasMany( c => c.Employees ).WithMany( e => e.Certificates )
                .Map( m => {
                    m.ToTable( "Employee_Certificate" );
                    m.MapLeftKey( "CertificateId" );
                    m.MapRightKey( "EmployeeId" );
                } );

What I was wondering is if I need to do an equivalent mapping for the Employee Entity as well?

Something like:

modelBuilder.Entity<Employee>()
            .HasMany( e => e.Certificates ).WithMany( c => c.Employees )
                .Map( m => {
                    m.ToTable( "Employee_Certificate" );
                    m.MapLeftKey( "EmployeeId" );
                    m.MapRightKey( "CertificateId" );
                } );

or will a simple HasMany(...).WithMany(...) be good enough?

GoldBishop
  • 2,820
  • 4
  • 47
  • 82
  • 1
    No you don't, it's enough to do it on one o the 2 entities – Raphael Dec 06 '13 at 10:11
  • thank you. What would the effect of doing it on both sides do? Would it be redundant without any repercussions or would it cause a potential N+1 situation? – GoldBishop Dec 07 '13 at 04:44
  • I've tried to create a db declaring the configuration twice, and it created the db as i expected, so the 2 configuration should coexist without problems. But personally I would keep just 1. – Raphael Dec 09 '13 at 06:45
  • Ideally, that would be great but as the Table Mapping is located in the Domain class file, I may not be able to control whether it has been declared once or twice. I migrated away from extending off the Model Builder and into a ClassMap design inheriting from the `EntityTypeConfiguration` class. – GoldBishop Dec 10 '13 at 13:48
  • Either way it works great. Thank you. – GoldBishop Dec 10 '13 at 13:49

0 Answers0