簡體   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