title: how do i make a background worker to update a datastore every 5 minutes in asp.net core
body: <p>I've followed the getting started tutorial and currently have a TODO CRUD app.</p>
  
  <p><a href="https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api-mac?view=aspnetcore-2.1" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api-mac?view=aspnetcore-2.1</a></p>
  
  <p>I want to add a background worker that updates the todo database every 5 minutes and sets Item 1 to a random value for its Name, and isCompleted properties.</p>
  
  <p>This is pretty easy in Java SpringBoot or Elixir's Phoenix...
  Is there a quick and painless way in c# to do this?</p>
  
  <p>The doc I found on Microsoft website was from 2012... so I assume there is a more elegant way to do this by now.</p>
  
  <p>Edit: I went with DNTScheduler.Core and it was relatively painless to set up. Followed the exampleApp setup that was on github repo and here is the task i ended up using:</p>
  
  <pre><code>using System;
  using System.Threading.Tasks;
  using DNTScheduler.Core.Contracts;
  using Microsoft.Extensions.Logging;
  using myapp.Models;
  
  namespace MyApp.Tasks
  {
      public class TaskOne : IScheduledTask
      {
          private readonly ILogger&lt;DoBackupTask&gt; _logger;
          private readonly TodoContext _context; // autoinjects since it was added in startup.cs in configure()
  
          public TaskOne(ILogger&lt;DoBackupTask&gt; logger, TodoContext context)
          {
              _logger = logger;
              _context = context;
          }
  
          public Task RunAsync()
          {
  
              var todo = _context.TodoItems.Find(1);
              if (todo == null)
              {
                  return Task.CompletedTask;
              }
  
              string[] names = new string[] { "val1", "val2"};
              Random r = new Random();
              string random_name = names[r.Next(0, names.Length)];
              todo.Name = random_name;
  
              _context.TodoItems.Update(todo);
              _context.SaveChanges();
              _logger.LogInformation("Ran My Task\n\n ");
  
              return Task.CompletedTask;
  
          }
      }
  }
  </code></pre>