title: Place the function that will scan the folder when asp.net application starts
body: <p>Asp.net MVC EF with Identity component.</p>
  
  <p>I need to retrieve all files from a directory and add them to the database when the application starts. Eg I can not put it in the HomeController because then it will be every time someone visits the page.</p>
  
  <p>This is the code I want to run.</p>
  
  <pre><code>DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/Images"));
              var folders = di.GetDirectories().ToList().Select(d =&gt; d.Name);
  
    var files = di.GetFiles();
              foreach (var file in files)
              {
                  var image = new Image{ name = file.ToString() };
                  db.Images.Add(image);
  }
  </code></pre>
  
  <p>I have tried to put it here</p>
  
  <pre><code>    public class ApplicationDbInitializer : DropCreateDatabaseAlways&lt;ApplicationDbContext&gt;
              {
      //Here
                  protected override void Seed(ApplicationDbContext context)
                  {
      //Here
      }
  }
  </code></pre>
  
  <p>But get error that I cant user <code>Server.MapPath</code></p>
  
  <p>Where should I put my code?</p>