EF 4.3反向代码优先:预先加载的相关实体为空

时间:2012-04-25 14:47:45

标签: entity-framework

我尝试使用导航属性访问某些相关实体。我已关闭延迟加载,并使用可用的Include()方法using System.Data.Entity来加载相关实体。但是,当视图加载时,我的相关实体为null。

我相信这些关系已经妥善配置,我错过了什么?

通话方式

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

上下文

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);
}
}

公司信息

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; } 
}

公司信息地图

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 个答案:

答案 0 :(得分:1)

好的我在使用Code First Database First创建的相同POCO创建Reverse Engineer Power Tool解决方案后想出了这个解决方案。

由于导航属性(相关实体)在构造函数上是新的,因此相关实体出现了null;这是覆盖实体并使其为空。

我不确定为什么Reverse Engineer Power Tool使用构造函数创建POCO,因为我没有必要使用Database First执行此操作,但构造函数可以全部删除在一起。

我之前无法捕捉到这一点,因为剃刀视图在相关的null实体上抛出了一个空引用异常。

HTH其他遇到同样问题的人。