Linq to SQL, InsertOnSubmit vs. InsertAllOnSubmit performance?
11,884
There is no difference at all, InsertAllOnSubmit actually calls the InsertOnSubmit, here is the code for InsertAllSubmit:
public void InsertAllOnSubmit<TSubEntity>(IEnumerable<TSubEntity> entities) where TSubEntity : TEntity
{
if (entities == null)
{
throw Error.ArgumentNull("entities");
}
this.CheckReadOnly();
this.context.CheckNotInSubmitChanges();
this.context.VerifyTrackingEnabled();
List<TSubEntity> list = entities.ToList<TSubEntity>();
using (List<TSubEntity>.Enumerator enumerator = list.GetEnumerator())
{
while (enumerator.MoveNext())
{
TEntity entity = (TEntity)((object)enumerator.Current);
this.InsertOnSubmit(entity);
}
}
}
As you can see InsertAllOnSubmit is just a handy wrapper around InsertOnSubmit
Related videos on Youtube
Author by
Randel Ramirez
{ C# | ASP.NET Core | Entity Framework Core | JavaScript | TypeScript | Web Applications | Mobile Applications | Xamarin | OOP } Get it to work. Make the code beautiful. Optimize.
Updated on September 18, 2022Comments
-
Randel Ramirez over 1 year
Is there a huge difference in performance between the two, for instance I have these two code snippets:
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(); }
vs
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(); }
Which among the two is better and does the two code snippet have the same number of trips to the database since in the 1st snippet submitchanges was called outside of the loop?
-
developer10214As far as I know, the InsertAllOnSubmit only iterates over a IEnumerable an calls InsertOnSubmit for each element. The important thing to call SubmitChanges inside or outside the loop, because each SubmitChanges call opens a new transaction/connection.
-