Birden fazla ilave birim aynı birincil anahtar olabilir
Burada 3 varlıkların modelim: Rota, Konum ve LocationİnRoute.
aşağıdaki yöntem başarısız olur ve özel durum olduğunda bunu elde etmesi için:
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;
}
Yaparken:
InsertRouteIfNotExists(companyId, locations);
UnitOfWork.Commit();
Var:
Unable to determine the principal end of the 'SimTaskModel.FK_T_STF_SUB_LOCATION_IN_ROUTE_T_STF_LOCATION_location_id' relationship. Multiple added entities may have the same primary key.
Ne zaman bölme taahhüt ve methos takın - çalışıyor:
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;
}
Bir kez tamamlama ve yöntemi dışında çağırmak istiyorum. Neden başarısız ilk örnekte ve bu özel durum ne demek?
CEVAP
Hata çözülmesi mümkün olmayan bir yabancı anahtar KİMLİĞİ başvurusu aksine) neden olur. Sizin durumunuzda, 0 KİMLİĞİ olan bir yere başvuran bir LocationİnRole var. Bu ID ile birden fazla yer var.
Yerleri henüz KİMLİĞİ oluşturulduğunda hangi veritabanına kaydedilmiş değil çünkü bir KİMLİĞİ atanmış değil. İkinci örnekte, bir Yerlerde bu işleri bu yüzden Kimliklerini erişilir önce kaydedilmiş.
Konum Kimlikleri hakkında ise sadece daha sonra SaveChanges isterseniz ilişkileri tanımlamak için güvenmek mümkün olmayacaktır.
Takas aşağıdaki satırı...
LocationId = locations[i].Id
...bu... ..
Location = locations[i]
İlişkiler daha sonra LocationİDs bağımlı olmayan nesne başvurularını dayalı olacaktır.
HashMap aynı anahtar altında birden fa...
Eclipse, görünümleri aynı anda birden ...
Yabancı anahtar kısıtlaması devir veya...
Birden fazla tablo üzerinde birincil a...
Tanıtımı YABANCI ANAHTAR kısıtlaması d...