这是我的3个实体模型:Route,Location和LocationInRoute.

以下方法失败并在提交时获取异常:
public static Route InsertRouteIfNotExists(Guid companyId, IListLocation> locations)
{
//Loop on locations and insert it without commit
InsertLocations(companyId, routesOrLocations);
RouteRepository routeRep = new RouteRepository();
Route route = routeRep.FindRoute(companyId, locations);
if (route == null)
{
route = new Route()
{
CompanyId = companyId,
IsDeleted = false
};
routeRep.Insert(route);
LocationInRouteRepository locInRouteRep = new LocationInRouteRepository();
for (int i = 0; i < locations.Count; i++)
{
locInRouteRep.Insert(new LocationInRoute()
{
//Id = i,
LocationId = locations[i].Id,
Order = i,
RouteId = route.Id
});
} …Run Code Online (Sandbox Code Playgroud) 我找不到关于究竟是什么让实体框架决定在设置外键时查找正确的相关对象的好文档.
我正在使用延迟加载(但不是更改跟踪)代理.设置外键然后获取导航属性值将返回null,即使相关的导航对象已加载并位于DbContext中也是如此.
调用DetectChanges有效,但看起来很重.有没有其他方法可以在Entity Framework中进行修复?