Entity framework 延迟加载在CodeFirst中不起作用

Entity framework 延迟加载在CodeFirst中不起作用,entity-framework,code-first,Entity Framework,Code First,} 存储库基类 public class EmployeeService : RepositoryBase, IEmployeeService { public EmployeeService(IDatabaseFactory DbFactory): base(DbFactory) { } 必须启用代理生成,并将集合属性定义为虚拟,才能使延迟加载工作。 此外,上下文应该保持活跃 public abstract class RepositoryBase { p

}

存储库基类

 public class EmployeeService : RepositoryBase, IEmployeeService
{
    public EmployeeService(IDatabaseFactory DbFactory): base(DbFactory)
    { }

必须启用代理生成,并将集合属性定义为虚拟,才能使延迟加载工作。 此外,上下文应该保持活跃

    public abstract class RepositoryBase 
{
    private CemexDb db;

    /// <summary>
    /// Holds a reference to the DatabaseFactory class used to manage connections to the database.
    /// </summary>
    protected IDatabaseFactory DatabaseFactory { get; private set; }
    /// <summary>
    /// Contains a reference to the <see cref="System.Data.Entity.DbContext"/> instance used by the repository.
    /// </summary>
    protected CemexDb CemexDb { get { return db ?? (db = DatabaseFactory.Get()); } }

    /// <summary>
    /// Initialises a new instance of the RepositoryBase class.
    /// </summary>
    /// <param name="DbFactory">A valid DatabaseFactory <see cref="Opendesk.Data.DatabaseFactory"/> object.</param>
    public RepositoryBase(IDatabaseFactory DbFactory)
    {
        DatabaseFactory = DbFactory;
    }

}

显示上下文的代码。还包括访问模式的整个片段。在访问employee对象的部门时是否保持上下文活动?我添加了上下文类。请看一看。我已经启用了代理创建。请查看代码public CemexDbstring connectionString:baseconnectionString{this.Configuration.LazyLoadingEnabled=true;this.Configuration.ProxyCreationEnabled=true;}但仍然不是working@Askar将此代码添加到问题中,并添加如何访问数据的示例代码。
 public class EmployeeService : RepositoryBase, IEmployeeService
{
    public EmployeeService(IDatabaseFactory DbFactory): base(DbFactory)
    { }
    public abstract class RepositoryBase 
{
    private CemexDb db;

    /// <summary>
    /// Holds a reference to the DatabaseFactory class used to manage connections to the database.
    /// </summary>
    protected IDatabaseFactory DatabaseFactory { get; private set; }
    /// <summary>
    /// Contains a reference to the <see cref="System.Data.Entity.DbContext"/> instance used by the repository.
    /// </summary>
    protected CemexDb CemexDb { get { return db ?? (db = DatabaseFactory.Get()); } }

    /// <summary>
    /// Initialises a new instance of the RepositoryBase class.
    /// </summary>
    /// <param name="DbFactory">A valid DatabaseFactory <see cref="Opendesk.Data.DatabaseFactory"/> object.</param>
    public RepositoryBase(IDatabaseFactory DbFactory)
    {
        DatabaseFactory = DbFactory;
    }

}
  public class CemexDb : DbContext
  {
     public CemexDb()
     {
        this.Configuration.ProxyCreationEnabled = true;
     } 

     public DbSet<Unicorn> Employees { get; set; }

  }