SORU
18 Mayıs 2011, ÇARŞAMBA


Birden fazla ilave birim aynı birincil anahtar olabilir

Burada 3 varlıkların modelim: Rota, Konum ve LocationİnRoute.
model

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
7 AĞUSTOS 2011, Pazar


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.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Tina Chen

    Tina Chen

    26 Mayıs 2012
  • TopDJMag TV

    TopDJMag TV

    29 Temmuz 2010
  • Trulia

    Trulia

    29 Kasım 2006