Search code examples
c#entity-frameworkdbcontextfluent-interface

How to name a map association with fluent api


So far I have built a fluent api association between the database tables.

example:

//Map Skill Associations
modelBuilder.Entity<Skill>()
    .HasMany( s => s.Employees ).WithMany( e => e.Skills )
    .Map( m => {
        m.ToTable( "Employee_Skill" );
        m.MapLeftKey( "SkillID" );
        m.MapRightKey( "EmployeeID" );
    } );

//Map Employee Associations
modelBuilder.Entity<Employee>()
    .HasMany( e => e.Skills ).WithMany( s => s.Employees )
    .Map( m => {
        m.ToTable( "Employee_Skill" );
        m.MapLeftKey( "EmployeeID" );
        m.MapRightKey( "SkillID" );
    } );

//Map Client Properties to Fields
modelBuilder.Entity<Employee>()
    .HasKey( e => e.EmployeeId).ToTable( "Employee" );
modelBuilder.Entity<Employee>()
    .Property( e => e.EmployeeId ).HasColumnName( "id" );
modelBuilder.Entity<Employee>()
    .Property( e => e.FirstName ).HasColumnName( "fname" );
modelBuilder.Entity<Employee>()
    .Property( e => e.LastName ).HasColumnName( "lname" );
modelBuilder.Entity<Employee>()
    .Property( e => e.LinkedIn ).HasColumnName( "linkedin" );
modelBuilder.Entity<Employee>()
    .Property( e => e.Active ).HasColumnName( "active" );
modelBuilder.Entity<Employee>()
    .Property( e => e.Inactive ).HasColumnName( "inactive" );
modelBuilder.Entity<Employee>()
    .Property( e => e.Created ).HasColumnName( "created" );

//Map Skill Properties to Fields
modelBuilder.Entity<Skill>()
    .HasKey(s => s.SkillId).ToTable( "Skill" );
modelBuilder.Entity<Skill>()
    .Property( s => s.SkillId ).HasColumnName( "id" );
modelBuilder.Entity<Skill>()
    .Property( s => s.Name ).HasColumnName( "name" );
modelBuilder.Entity<Skill>()
    .Property( s => s.Description ).HasColumnName( "description" );
modelBuilder.Entity<Skill>()
    .Property( s => s.Active ).HasColumnName( "active" );
modelBuilder.Entity<Skill>()
    .Property( s => s.Inactive ).HasColumnName( "inactive" );

In the past I have dealt with the DBMX file and done the visual association between the entity's. In code when I wanted to load the association I would do something like Context.Employees.Include("Skills") to make sure that the Employee entity would have the associated Skills loaded in the return Collection.

With Fluent, how can I use the same syntax structure to load (think its called LazyLoading) the Collection objects when needed but otherwise do not auto load them.

I have not seem any option that allows me to name the Skills or Employees association to the respective entities.

Any help would be greatly appreciated.


Solution

  • To use LazyLoading you should define your properties as virtual in model classes. No changes in Fluent configuration are required:

    class Skill
    {
        ...
        public virtual List<Employee> Employees { get; set; }
    }
    
    class Employee
    {
        ...
        public virtual List<Skill> Skills { get; set; }
    }
    

    Read more about LazyLoading here