我遇到了将重复数据插入数据库的问题,我是否在IEnumerable<Location>
中传递了错误的参数?
调试应用程序时不会出现任何错误。
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();
}
答案 0 :(得分:4)
我猜你不是故意这样做的:
newLocation.FacebookID = locationID;
而是这个:
newLocation.FacebookID = facebookID;
基本上你创建的是多条记录,使用相同的facebookId,而不是实际使用的是locationID。