0

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.

GoldBishop
  • 2,820
  • 4
  • 47
  • 82

1 Answers1

1

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

alexmac
  • 19,087
  • 7
  • 58
  • 69
  • you say to use `virtual` but your code samples are missing the `virtual` keyword – Claies Nov 20 '13 at 19:37
  • @Alexander: I understand what you mean. Had toyed with the idea of that. Refresh my memory, Virtual methods are only loaded with data when I access the data portion correct? Something like `Employee.Skills`, or do I need to do the `Include("Skills")` to load the property with data? – GoldBishop Nov 20 '13 at 19:41
  • Sorry, forgot virtual) – alexmac Nov 20 '13 at 19:41
  • I am trying to keep my data load on memory, to a minimum if possible. – GoldBishop Nov 20 '13 at 19:42
  • @GoldBishop, exactly. Until, you do not call the Employee.Skills, the Skills objects are not be loaded. – alexmac Nov 20 '13 at 19:43
  • @Alexander Out of Scope, but what kind of loading requires you to use the Include method to load the child collection? – GoldBishop Nov 20 '13 at 19:54
  • Read [this answer](http://stackoverflow.com/questions/3505170/entity-framework-whats-the-difference-between-using-include-eager-loading-and), here very good described diffs between LL and Include. – alexmac Nov 20 '13 at 19:59