多个添加的实体可以具有相同的主键

Nao*_*aor 75 .net c# entity-framework

这是我的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
                    });
                }
            }
            return route;
        }
Run Code Online (Sandbox Code Playgroud)

做的时候:

InsertRouteIfNotExists(companyId, locations);
UnitOfWork.Commit();
Run Code Online (Sandbox Code Playgroud)

我有:

无法确定'SimTaskModel.FK_T_STF_SUB_LOCATION_IN_ROUTE_T_STF_LOCATION_location_id'关系的主要结尾.多个添加的实体可以具有相同的主键.

拆分提交并插入到方法中时 - 它可以工作:

  public static Route InsertRouteIfNotExists(Guid companyId, IListLocation> locations)
            {
                //Loop on locations and insert it without commit
                InsertLocations(companyId, routesOrLocations);
                UnitOfWork.Commit();

                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
                        });
                    }
                    UnitOfWork.Commit();
                }
                return route;
            }
Run Code Online (Sandbox Code Playgroud)

我想在方法之外调用一次提交.为什么它在第一个例子中失败了,这个例外意味着什么?

Sco*_*nro 135

该错误是由无法解析的外键ID(与引用相对)引起的.在您的情况下,您有一个LocationInRole引用ID为0的位置.有多个具有此ID的位置.

尚未为位置分配ID,因为它们尚未保存到生成ID的数据库中.在第二个示例中,在访问ID之前保存位置,这就是为什么这样做的原因.

如果您希望稍后再保存SaveChanges,则无法依赖位置ID来定义关系.

交换以下行...

LocationId = locations[i].Id
Run Code Online (Sandbox Code Playgroud)

...为了这...

Location = locations[i]
Run Code Online (Sandbox Code Playgroud)

然后,关系将基于不依赖于LocationID的对象引用.