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