开发者

C# Duplicate Data Problem

I am having problems with duplicate data being inserted in to the database, am I passing a wrong parameter in the IE开发者_开发百科numerable<Location>?

It doesn't bring up any errors when I debug the app.

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();
}


I am guessing you did not mean to do this:

newLocation.FacebookID = locationID;

But rather this:

newLocation.FacebookID = facebookID;

Basically you are creating multiple records, with the same facebookId, as you actually used the locationID instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜