C# 重复数据问题

发布于 2024-12-07 13:10:55 字数 944 浏览 0 评论 0原文

我在将重复数据插入数据库时​​遇到问题,是否在 IEnumerable 中传递了错误的参数?

当我调试应用程序时,它不会出现任何错误。

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 having problems with duplicate data being inserted in to the database, am I passing a wrong parameter in the IEnumerable<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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人海汹涌 2024-12-14 13:10:55

我猜你并不是故意这样做的:

newLocation.FacebookID = locationID;

而是这样的:

newLocation.FacebookID = facebookID;

基本上你正在使用相同的 facebookId 创建多个记录,因为你实际上使用了 locationID。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文