2011-09-28 74 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取得了诀窍:) –