Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 检索新插入的项时虚拟属性为null_.net_Entity Framework_Entity - Fatal编程技术网

.net 检索新插入的项时虚拟属性为null

.net 检索新插入的项时虚拟属性为null,.net,entity-framework,entity,.net,Entity Framework,Entity,我正在使用EF4.x,代码优先和Castle Windsor for DI。我的问题:检索新插入的实体时,某些虚拟属性返回空值。我是新来的,所以请原谅我对一切工作方式的无知。一个非常简单的例子如下: public class Address { public int AddressID { get; set; } public string Street { get; set; } public int ProvinceID { get; set; } publi

我正在使用EF4.x,代码优先和Castle Windsor for DI。我的问题:检索新插入的实体时,某些虚拟属性返回空值。我是新来的,所以请原谅我对一切工作方式的无知。一个非常简单的例子如下:

public class Address
{
    public int AddressID { get; set; }
    public string Street { get; set; }
    public int ProvinceID { get; set; }
    public virtual Province { get; set; }
    etc....
}

public class Province
{
    public int ProvinceID { get; set; }
    public string Name { get; set; }
    public string Abbreviation { get; set; }
    public int DisplayOrder { get; set; }
    etc...
}
_dbSet = ((DbContext)_context).Set<T>(); // _context is of IDbContext
因此,在SaveChanges()之后,我可以看到在数据库中正确创建的记录,但是在加载地址实体的后续页面请求中,ProvinceID设置正确,而virtual Province为null。重建后,没有问题。我做错了什么?短暂性脑缺血发作

更多信息:

我在存储库和工作单元模式中使用DbContext。我有一个抽象的EF RepositoryBase类,它包含所有常见的CRUD方法。以下是常见GET方法的示例:

  public T Get(params object[] keyValues)
  {
     return _dbSet.Find(keyValues);
  }

  public IQueryable<T> Get(Func<T, bool> filter)
  {
     return _dbSet.Where(filter).AsQueryable();
  }

  public IQueryable<T> GetAll()
  {
     return _dbSet.AsQueryable();
  }
public T-Get(params-object[]keyValues)
{
返回_dbSet.Find(keyValues);
}
公共IQueryable获取(Func筛选器)
{
返回_dbSet.Where(filter.AsQueryable();
}
公共IQueryable GetAll()
{
返回_dbSet.AsQueryable();
}
_dbSet的设置如下:

public class Address
{
    public int AddressID { get; set; }
    public string Street { get; set; }
    public int ProvinceID { get; set; }
    public virtual Province { get; set; }
    etc....
}

public class Province
{
    public int ProvinceID { get; set; }
    public string Name { get; set; }
    public string Abbreviation { get; set; }
    public int DisplayOrder { get; set; }
    etc...
}
_dbSet = ((DbContext)_context).Set<T>(); // _context is of IDbContext
\u dbSet=((DbContext)\u context.Set();//_语境就是语境

可能是这里有什么原因导致了这个奇怪的问题?

因此,实际上您有两个EF选项,分别是导航属性加载、延迟加载和显式加载。根据我的经验,显式加载要好得多,因为它更难导致重大性能问题

我写了一篇关于导航属性的文章,你可以阅读

要使用显式导航属性加载,请使用。包含语句可指定在每个查询中加载哪些属性


当您使用lazyloading时,我相信您需要虚拟装饰您上下文中的所有导航属性,并且当您调用属性上的get时会查询数据库。

我使用下一个解决方法:分离实体并再次加载

public T Reload<T>(T entity) where T : class, IEntityId
{
    ((IObjectContextAdapter)_dbContext).ObjectContext.Detach(entity);
    return _dbContext.Set<T>().FirstOrDefault(x => x.Id == entity.Id);
}
public T重载(T实体),其中T:class,IEntityId
{
((IObjectContextAdapter)dbContext.ObjectContext.Detach(实体);
返回_dbContext.Set().FirstOrDefault(x=>x.Id==entity.Id);
}

您是否启用了延迟加载?默认情况下是否启用了延迟加载?此外,其他导航属性也可以正常工作,即使是这个属性也只能在检索新插入的记录时失败。在下一次重建中,这也开始工作。您需要启用代理创建。谢谢。这是一篇很棒的文章,我会考虑你的建议。但我想先弄清楚为什么我会有这个问题。