2011-09-28 73 views
0

我有重複的數據被插入到數據庫的問題,我在IEnumerable<Location>傳遞錯誤的參數?C#重複數據問題

它調試應用程序時不會出現任何錯誤。

IEnumerable<Location> locations = context.Locations.Where(l => l.FacebookID == facebookID); 

if (locations.Count() == 0) 
{ 
    Location newLocation = new Location(); 

    newLocation.FacebookID = locationID; 

    newLocation.Description = locationValue; 

    IGeoCoder geoCoder = new GoogleGeoCoder(GoogleAPIKey); 
    Address[] addresses = geoCoder.GeoCode(locationValue); 

    if (addresses.Length > 0) 
    { 
     // Let's assume the first one is good enough 
     Address address = addresses[0]; 

     newLocation.Latitude= address.Coordinates.Latitude.ToString(); 
     newLocation.Longitude = address.Coordinates.Longitude.ToString(); 
     // Use location.Latitude and location.Longitude 

    } 

    context.Locations.AddObject(newLocation); 
    context.SaveChanges(); 
} 

回答

4

我猜你不是故意這樣做:

newLocation.FacebookID = locationID; 

而恰恰這一點:

newLocation.FacebookID = facebookID; 

基本上你正在創建多個記錄,與同facebookId,因爲你實際上使用了locationID。

+0

IEnumerable locations = context.Locations.Where(l => l.FacebookID == locationID);似乎爲我做了訣竅,將其從facebook = facebookid更改爲locationID取得了訣竅:) –