這是我的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_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一次和方法之外。爲什麼它在第一個例子中失敗了,這個例外意味着什麼?
@Ladislav Mrnka:我沒有老闆,這是我的項目。我真的不知道你的印象是我立即問到的。你不是唯一一個整天使用電腦的人。免費諮詢?有人爲他的答案提供任何保證嗎?我相信這是一個論壇,可以在這裏問問題,這就是我正在做的事情。我有很多問題,我相信我會通過這個論壇和像你這樣的人來進行長期的學習。參與是一個選擇。 – Naor 2011-05-18 07:35:33
@拉迪斯拉夫:我只看到一個相當好的問題,而且OP的個人資料也沒有顯示任何事情。 – 2011-05-18 09:22:24
您是否在整個操作範圍內使用相同的ObjectContext,或者每個新的Repository都有自己的ObjectContext? – 2011-05-19 15:51:59