How expensive is Entity Framework SaveChanges? Auto-Save vs. explicit [context.SaveChanges on Dispose]

Viewed 1499

One question I have been wondering is, should I call context.SaveChanges() explicitly or let it be under Dispose().

Approach 1: Auto-Save

    public virtual void Dispose()
    {
        context.SaveChanges();
        context.Dispose();
    }

Let's say we add user into UserRepository (or using dependency injection)

  using (var repo = new UserRepository())
  {
       repo.Add(user);
  }

Approach 2: explicit save

    public virtual void SaveChanges()
    {
        context.SaveChanges();
    }

    public virtual void Dispose()
    {
        context.Dispose();
    }

Let's say we add user into UserRepository (or using dependency injection)

  using (var repo = new UserRepository())
  {
       repo.Add(user);
       repo.SaveChanges();
  }

So the question I have is: the Auto-Save produces cleaner code, but how expensive is the operation? Which is the better approach?

1 Answers
Related