2011-05-18 106 views
60

這是我的3個實體模型:Route,Location和LocationInRoute。
model多個添加的實體可能具有相同的主鍵

以下方法失敗,當提交它得到的異常:

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_LOCATI'的ON_location_id'關係。多個添加的實體可能具有相同的主鍵。

在分割提交併插入到methos - 它的工作原理:

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一次和方法之外。爲什麼它在第一個例子中失敗了,這個例外意味着什麼?

+4

@Ladislav Mrnka:我沒有老闆,這是我的項目。我真的不知道你的印象是我立即問到的。你不是唯一一個整天使用電腦的人。免費諮詢?有人爲他的答案提供任何保證嗎?我相信這是一個論壇,可以在這裏問問題,這就是我正在做的事情。我有很多問題,我相信我會通過這個論壇和像你這樣的人來進行長期的學習。參與是一個選擇。 – Naor 2011-05-18 07:35:33

+0

@拉迪斯拉夫:我只看到一個相當好的問題,而且OP的個人資料也沒有顯示任何事情。 – 2011-05-18 09:22:24

+0

您是否在整個操作範圍內使用相同的ObjectContext,或者每個新的Repository都有自己的ObjectContext? – 2011-05-19 15:51:59

回答

123

該錯誤是由無法解析的外鍵ID(而不是參考)引起的。在你的情況下,你有一個LocationInRole引用一個ID爲0的位置。有多個位置使用這個ID。

位置還沒有被分配一個ID,因爲他們還沒有被保存到生成ID時的數據庫。在你的第二個例子中,位置在它們的ID被訪問之前被保存,這就是爲什麼這是有效的。

如果您稍後想要SaveChanges,您將無法依靠位置ID來定義關係。

交換下面的行...

LocationId = locations[i].Id 

...這個...

Location = locations[i] 

的關係將被基於對象的引用這是不依賴於LocationIDs。

+0

你也節省了我的一天:) – senzacionale 2014-10-10 15:10:42

+0

你們兩個都可以看看我的文章,並告訴我如何解決它,我得到同樣的問題:http://stackoverflow.com/questions/26783934/foreign-密鑰循環或級聯路徑我很感激它! – 2014-11-06 23:44:10

+0

謝謝,它真的有幫助:-) – user261002 2014-12-04 13:44:06

4

如果這對未來的讀者有任何用處,在我的情況下,這個錯誤是由於在我的數據庫(和從DB生成的模型)中配置不正確的外鍵。

我有表:

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

和孫表無意中收到一個外鍵到它的父(兒童)和它的祖父(母)。在保存新的多個Parent實體時,我收到了這個錯誤。修復一直在糾正外鍵。

+0

在我的情況下,我(愚蠢)將我的主鍵基表設置爲與我的外鍵基表和我的主鍵列相同我的外鍵列*弓頭羞辱* 希望這可以幫助別人.. – Dave 2016-08-16 13:21:26

0

遇到同樣的錯誤我高度懷疑實際問題是定位的位置。簡單地說,在EF代碼首先,我敢打賭,它看起來像這樣:

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

換句話說,在問題,ParentLocation/ParentLocationId是一個遞歸引用回到這個表。

ParentLocationId不可爲空。這意味着它將被插入一個0,並且EF會在Insert上投訴,而不是在你遷移時 - 即使事實是遷移運行一次,你有一張表EF永遠不會讓你插入。

做一個遞歸引用回到同一表工作的唯一方法是使遞歸引用可爲空:

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

注意?int後。

+0

Wouldn如果在創建子位置之前在父位置上執行SaveChanges,那麼舊的(不可爲空)版本工作? – 2017-06-22 14:56:58

+1

忽略以上,我是個白癡。您將無法創建第一個位置。 – 2017-06-22 14:58:48

0

對於那些尋找這個例外:
在我的情況下,它沒有設置所需的導航屬性。

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 
0

我有同樣的問題。以下情景爲我解決。 我想你必須改變你的代碼如下:

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

insertedRoute.LocationInRoute.Add(lInRoute); 
} 
相關問題