C# 重复数据问题
我在将重复数据插入数据库时遇到问题,是否在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜你并不是故意这样做的:
而是这样的:
基本上你正在使用相同的 facebookId 创建多个记录,因为你实际上使用了 locationID。
I am guessing you did not mean to do this:
But rather this:
Basically you are creating multiple records, with the same facebookId, as you actually used the locationID instead.