C# 代码优先的实体框架未初始化外键类成员

C# 代码优先的实体框架未初始化外键类成员,c#,entity-framework-6,ef-code-first,C#,Entity Framework 6,Ef Code First,我使用的是EF6,代码优先,我有一个域模型类,它有两个FK和相关对象作为类成员。插入工作正常,但从数据库中获取值时,与此对象相关的FKs对象为空 我尝试将延迟初始化设置为false,并将FK类成员标记为虚拟。我已经迫不及待地尝试加载,但仍然没有任何积极的结果(反正我也不想迫不及待地使用) 公共类影响位置 { 受公众影响的地点() { Id=Guid.NewGuid(); } [关键] [必需] 公共Guid Id{get;set;} [必需] 公共字符串名称{get;set;} 公共字符串说明{

我使用的是EF6,代码优先,我有一个域模型类,它有两个FK和相关对象作为类成员。插入工作正常,但从数据库中获取值时,与此对象相关的FKs对象为空

我尝试将延迟初始化设置为false,并将FK类成员标记为虚拟。我已经迫不及待地尝试加载,但仍然没有任何积极的结果(反正我也不想迫不及待地使用)

公共类影响位置
{
受公众影响的地点()
{
Id=Guid.NewGuid();
}
[关键]
[必需]
公共Guid Id{get;set;}
[必需]
公共字符串名称{get;set;}
公共字符串说明{get;set;}
[必需]
[外键(“辐射级”)]
公共Guid FK_辐射级别{get;set;}
公共虚拟辐射级别辐射级别{get;set;}
[必需]
[外键(“地理坐标”)]
公共Guid FK_地理坐标{get;set;}
公共虚拟地理坐标地理坐标{get;set;
}
公共级辐射水平
{
公共辐射水平
{
Id=Guid.NewGuid();
}
[关键]
公共Guid Id{get;set;}
[必需]
公共整数级别{get;set;}
公共字符串说明{get;set;}
}
公共类地理坐标
{
公共地理坐标()
{
Id=Guid.NewGuid();
}
[关键]
公共Guid Id{get;set;}
[必需]
公共浮点纬度{get;set;}
[必需]
公共浮点经度{get;set;}
[必需]
公共浮点范围{get;set;}
}
公共类存储库:IRepository,其中tenty:class
{
受保护的只读DbContext上下文;
公共存储库(DbContext上下文)
{
上下文=上下文;
}
公共IEnumerable GetAll()
{
返回Context.Set().ToList();
}
}
公共类LocationRepository:Repository,ILocationRepository
{
公共位置存储库(RadioactiveAreaContext上下文)
:基本(上下文)
{
}
}
公共类UnitOfWork:IUnitOfWork
{
私有只读RadioactiveAreaContext\u context;
公共工作单元()
{
_context=新的RadioactiveAreaContext();
位置=新位置存储库(_上下文);
}
公共ILocationRepository位置{get;private set;}
公共int Complete()
{
返回_context.SaveChanges();
}
公共空间处置()
{
_context.Dispose();
}
}
公共类RadioactiveAreaContext:DbContext
{
公共放射性区域上下文()
:base(“名称=RadioactiveAreaContext”)
{
this.Configuration.LazyLoadingEnabled=false;
}
公共虚拟数据库集位置{get;set;}
公共虚拟数据库集辐射级别{get;set;}
公共虚拟数据库集地理坐标{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(newlocationconfiguration());
添加(新的RadiationLevel配置());
modelBuilder.Configurations.Add(新的地理坐标配置());
}
}
var unitOfWork=新的unitOfWork();
unitOfWork.Location.Add(someLocation);
unitOfWork.Complete();
var locations=unitOfWork.Location.GetAll();//将从数据库正确加载第一个位置,请查看第一张图片(值不为null)
//添加一个新位置并再次调用GetAll(),现在将给出2个结果,但第二个结果是EF动态代理,并且没有加载FK类成员。。。
请查看这些图片,在第一次插入后获取数据是可以的,但在第二次插入后,获取数据时,将发生以下情况:


当您第一次保存位置并尝试获取所有位置时,它将正确加载到上下文中,因为您将其添加到同一上下文中, 第二次相关实体将为空,因为它是新上下文

这是因为未启用延迟加载

 this.Configuration.LazyLoadingEnabled = false;
要获取相关项目,您应该包括它们

1-将GetAllWithInclude添加到您的基本
存储库

public IEnumerable<TEntity> GetAllWithInclude(List<string> includes)
{
    IQueryable<TEntity> entities = Context.Set<TEntity>();
    foreach (var include in includes)
    {
        entities = entities.Include(include);
    }
    return entities.ToList();
} 
public IEnumerable GetAllWithInclude(列表包含)
{
IQueryable entities=Context.Set();
foreach(包含在包含中的var)
{
实体=实体。包括(Include);
}
返回实体。ToList();
} 
2-将GetAllWithInclude添加到ILocationRepository接口

IEnumerable<AffectedLocation> GetAllWithInclude(List<string> includes);
IEnumerable GetAllWithInclude(列表包含);
3-若要获取数据,请将要包含的子实体添加到包含列表中

List<string> includes = new List<string>
{
    "RadiationLevel",
    "GeographicalCoordinates"
};

List<AffectedLocation> affectedLocations = uow.Location.GetAllWithInclude(includes).ToList();
List includes=新列表
{
“辐射水平”,
“地理坐标”
};
List affectedLocations=uow.Location.GetAllWithInclude(includes.ToList();

当您第一次保存位置并尝试获取所有位置时,它将正确加载到上下文中,因为您将其添加到同一上下文中, 第二次相关实体将为空,因为它是新上下文

这是因为未启用延迟加载

 this.Configuration.LazyLoadingEnabled = false;
要获取相关项目,您应该包括它们

1-将GetAllWithInclude添加到您的基本
存储库

public IEnumerable<TEntity> GetAllWithInclude(List<string> includes)
{
    IQueryable<TEntity> entities = Context.Set<TEntity>();
    foreach (var include in includes)
    {
        entities = entities.Include(include);
    }
    return entities.ToList();
} 
public IEnumerable GetAllWithInclude(列表包含)
{
IQueryable entities=Context.Set();
foreach(包含在包含中的var)
{
实体=实体。包括(Include);
}
返回实体。ToList();
} 
2-将GetAllWithInclude添加到ILocationRepository接口

IEnumerable<AffectedLocation> GetAllWithInclude(List<string> includes);
IEnumerable GetAllWithInclude(列表包含);
3-若要获取数据,请将要包含的子实体添加到包含列表中

List<string> includes = new List<string>
{
    "RadiationLevel",
    "GeographicalCoordinates"
};

List<AffectedLocation> affectedLocations = uow.Location.GetAllWithInclude(includes).ToList();
List includes=新列表
{
“辐射水平”,
“地理坐标”
};
List affectedLocations=uow.Location.GetAllWithInclude(includes.ToList();

我已经启用了延迟初始化,一切正常n