1

I'm trying to access some related entities using navigation properties. I've turned off Lazy Loading and am using the Include() method available using System.Data.Entity to Eager Load the related entities. However my related entities are null when the view loads.

I believe the relationships have been properly configured what am I missing?

Calling Method

public IQueryable<JobRecord> GetAll()
{
    return _dataContext.Set<JobRecord>()
       .Include(t => t.CompanyInfo);
}

Context

public class Application_Context : DbContext, IDataContext
{
public Gyroview_Context():base("Application_Entities")
{ 
    Database.SetInitializer<Application_Context>(null);
    Configuration.LazyLoadingEnabled = false;
}

    public DbSet<CompanyInfo> CompanyInfoes { get; set; }
public DbSet<JobRecord> JobRecords { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new CompanyInfoMap());
            modelBuilder.Configurations.Add(new JobRecordMap());

    modelBuilder.Entity<JobRecord>()
            .HasOptional(r => r.CompanyInfo)
            .WithMany(c => c.JobRecords)
            .HasForeignKey(r => r.CompanyID);
}

public IDbSet<TEntity> Set<TEntity>() where TEntity : class
{
    return base.Set<TEntity>();
}
}

JobRecord

public class JobRecord
{
public JobRecord()
{
        this.CompanyInfo = new CompanyInfo();
}

    public Nullable<int> CompanyID { get; set; }
public int Jobid { get; set; }

public virtual CompanyInfo CompanyInfo { get; set; }
}

JobRecord Map

public class JobRecordMap : EntityTypeConfiguration<JobRecord>
{
    public JobRecordMap()
    {
    // Primary Key
    this.HasKey(t => t.Jobid);

    // Table & Column Mappings
    this.ToTable("JobRecord");
    this.Property(t => t.CompanyID).HasColumnName("CompanyID");
    this.Property(t => t.Jobid).HasColumnName("Jobid");

    // Relationships    

        this.HasOptional(t => t.CompanyInfo)
    .WithMany(t => t.JobRecords)
            .HasForeignKey(d => d.CompanyID);
}
}

CompanyInfo

public class CompanyInfo
{
    public CompanyInfo()
    {
        this.JobRecords = new List<JobRecord>();    
    }

    public int CompanyID { get; set; }
    public string CompanyName { get; set; }
    public string Address { get; set; }

    public virtual ICollection<JobRecord> JobRecords { get; set; } 
}

CompanyInfo Map

public class CompanyInfoMap : EntityTypeConfiguration<RigViewCompanyInfo>
{
    public CompanyInfoMap()
    {
    // Primary Key
    this.HasKey(t => t.CompanyID);

    // Properties
    this.Property(t => t.CompanyName)
    .HasMaxLength(255);

    this.Property(t => t.Address)
    .HasMaxLength(255);


    // Table & Column Mappings
    this.Property(t => t.CompanyID).HasColumnName("CompanyID");
    this.ToTable("CompanyInfo");
    this.Property(t => t.CompanyName).HasColumnName("CompanyName");
    this.Property(t => t.Address).HasColumnName("Address");

}
 }

1 Answer 1

1

Ok I figured this one out after creating a Database First solution using the same POCOs created by the Code First Reverse Engineer Power Tool.

The related entities were coming out null due to the navigation property (related entity) being new'd up on the constructor; this was overwriting the entity and making it null.

I'm not sure why the Reverse Engineer Power Tool creates the POCO with the constructor as I've not had the need to do this with Database First, but the constructor can be removed all together.

I was not able to catch this earlier because the razor view was throwing a null reference exception on the related entities that were null.

HTH anyone else experiencing the same problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.