यहाँ 3 संस्थाओं का मेरा मॉडल है: मार्ग, स्थान और स्थान- InRoute।

निम्न विधि विफल हो जाती है और जब यह हो तो अपवाद प्राप्त करें:
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;
}
मैं विधि के बाहर और भीतर एक बार कॉल करना चाहूंगा। यह पहले उदाहरण में क्यों विफल हो जाता है और इसका अपवाद क्या है?