繁体   English   中英

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

[英]Multiple added entities may have the same primary key

这是我的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;
        }

进行时:

InsertRouteIfNotExists(companyId, locations);
UnitOfWork.Commit();

我有:

无法确定“ 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;
            }

我想在方法外调用一次commit。 为什么在第一个示例中失败,该异常意味着什么?

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

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

如果您只想稍后保存更改,则将不能依靠位置ID来定义关系。

交换以下行...

LocationId = locations[i].Id

...为了这...

Location = locations[i]

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

如果这对以后的读者有用,那么在我的情况下,此错误是由于数据库(以及从数据库生成的模型)中的外键配置不正确引起的。

我有桌子:

Parent (1-1) Child (1-many) Grandchild

并且孙子表在不经意间收到了一个外键,直到它的父母(孩子)和祖父母(父母)为止。 从新保存多个父实体时,我收到此错误。 已修复了更正外键的问题。

遇到相同的错误后,我高度怀疑实际问题是位置的定义。 简而言之,我敢打赌,它看起来像这样:

public class Location
{
    public int Id { get; set; }
    ...
    public Location ParentLocation { get; set; }
    [ForeignKey("ParentLocation")]
    public int ParentLocationId { get; set; }
}

换句话说,在Question中,ParentLocation / ParentLocationId是对该表的递归引用。

ParentLocationId不可为空。 这意味着它将插入一个0,并且EF将在插入时(而不是在您进行迁移时)进行抱怨-即使事实是一旦运行Migration,您就有一个表EF永远不会让您插入。

使递归引用返回到同一表工作的唯一方法是使递归引用可为空:

public class Location
{
    public int Id { get; set; }
    ...
    public Location ParentLocation { get; set; }
    [ForeignKey("ParentLocation")]
    public int? ParentLocationId { get; set; }
}

注意? int

对于那些搜索此异常的人:
就我而言,它无法设置必需的导航属性。

public class Question
{
    //...
    public int QuestionGridItemID { get; set; }
    public virtual QuestionGridItem GridItem { get; set; }
    //...
    public int? OtherQuestionID { get; set; }
    public Question OtherQuestion { get; set; }
}

//...

question.OtherQuestion = otherQuestion;
questionGridItem.Questions.Add(question);
dataContext.SaveChanges(); //fails because otherQuestion wasn't added to 
//any grid item's Question collection

我有同样的问题。 以下情况为我解决了。 我认为您必须更改您的代码,如下所示:

var insertedRoute =routeRep.Insert(route);
.....
insertedRoute.LocationInRoute = new List<LocationInRoute>();
for(....){
    var lInRoute = new LocationInRoute(){
    ....
    Route=insertedRoute;
}

insertedRoute.LocationInRoute.Add(lInRoute );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM