两者之间的性能是否存在巨大差异,例如我有这两个代码片段:
public void Insert(IEnumerable<ManageGeofenceViewModel> geofences)
{
var insertGeofences = new List<Geofence>();
foreach(var geofence in geofences)
{
Geofence insertGeofence = new Geofence
{
name = geofence.Name,
radius = geofence.Meters,
latitude = geofence.Latitude,
longitude = geofence.Longitude,
custom_point_type_id = geofence.CategoryID
};
insertGeofences.Add(insertGeofence);
}
this.context.Geofences.InsertAllOnSubmit(insertGeofences);
this.context.SubmitChanges();
}
对比
public void Insert(IEnumerable<ManageGeofenceViewModel> geofences)
{
foreach(var geofence in geofences)
{
Geofence insertGeofence = new Geofence
{
name = geofence.Name,
radius = geofence.Meters,
latitude = geofence.Latitude,
longitude = geofence.Longitude,
custom_point_type_id = geofence.CategoryID
};
this.context.Geofences.InsertOnSubmit(insertGeofence);
}
this.context.SubmitChanges();
}
两者中哪一个更好,并且由于在第一个代码段中 submitchanges 在循环外被调用,这两个代码片段是否具有相同的数据库访问次数?