title: .Net Core Web API with EF
body: <p>I am brand new to both .Net Core and to Entity Framework. I am trying to learn both technologies, by following some tutorial, but I am struggling. My Web API project has two controllers - the wizard generated ValuesController.cs that gets created when creating a new Core Web API project, and a second controller I added called FilesController.cs.</p>
  
  <p>My appsettings.json:</p>
  
  <pre><code>{
    "Logging": {
      "IncludeScopes": false,
      "LogLevel": {
        "Default": "Debug",
        "System": "Information",
        "Microsoft": "Information"
      }
    },
  
    "ConnectionStrings": {
      "    \"PropWorxConnection\"": "server=a.b.com;user id=propworx;persistsecurityinfo=True;database=abcde;password=12345"
    }
  }
  </code></pre>
  
  <p>My Startup.cs</p>
  
  <pre><code>using Microsoft.AspNetCore.Builder;
  using Microsoft.AspNetCore.Hosting;
  using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.DependencyInjection;
  using Microsoft.Extensions.Logging;
  using MySQL.Data.EntityFrameworkCore.Extensions;
  
  namespace PropWorxAPI
  {
      public class Startup
      {
          public Startup(IHostingEnvironment env)
          {
              var builder = new ConfigurationBuilder()
                  .SetBasePath(env.ContentRootPath)
                  .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                  .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                  .AddEnvironmentVariables();
              Configuration = builder.Build();
          }
  
          public IConfigurationRoot Configuration { get; }
  
          public void ConfigureServices(IServiceCollection services)
          {
              services.AddMvc();
              services.AddDbContext&lt;Models.PropWorxDbContext&gt;(options =&gt;
                  options.UseMySQL(Configuration.GetConnectionString("PropWorxConnection")));
          }
  
          public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
          {
              loggerFactory.AddConsole(Configuration.GetSection("Logging"));
              loggerFactory.AddDebug();
  
              app.UseMvc();
          }
      }
  }
  </code></pre>
  
  <p>A simple model:</p>
  
  <pre><code>namespace PropWorxAPI.Models
  {
      [Table("files")]
      public partial class File
      {
          public int ID { get; set; }
          public string FileNum { get; set; }
      }
  }
  </code></pre>
  
  <p>My Context:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using Microsoft.EntityFrameworkCore;
  using MySQL.Data.EntityFrameworkCore.Extensions;
  
  namespace PropWorxAPI.Models
  {
      public class PropWorxDbContext : DbContext
      {
          private readonly string _connectionString;
  
          public PropWorxDbContext(string connectionString)
          {
              _connectionString = connectionString;
          }
  
          public PropWorxDbContext(DbContextOptions&lt;PropWorxDbContext&gt; options) : base(options)
          {
          }
  
          public DbSet&lt;Models.File&gt; Files { get; set; }
  
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              modelBuilder.Entity&lt;Models.File&gt;().ToTable("Files");
          }
  
          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
          {
              optionsBuilder.UseMySQL(_connectionString);
              base.OnConfiguring(optionsBuilder);
          }
      }
  }
  </code></pre>
  
  <p>and my controller:</p>
  
  <pre><code>using Microsoft.AspNetCore.Mvc;
  using PropWorxAPI.Models;
  
  namespace PropWorxAPI.Controllers
  {
      [Route("api/[controller]")]
      public class FilesController : Controller
      {
          private readonly PropWorxDbContext _context;
  
          public FilesController(PropWorxDbContext context)
          {
              _context = context;
          }
  
          [HttpGet]
          public string Get()
          {
              return "Controller Working!!!";
          }
      }
  }
  </code></pre>
  
  <p>Now, when I run it (F5), it opens up Edge and goes to <em><a href="http://localhost:51932/api/values" rel="nofollow noreferrer">http://localhost:51932/api/values</a></em>. This displays this output:</p>
  
  <p><strong>["value1","value2"]</strong></p>
  
  <p>So it's defaulting to <em>ValuesController</em> (How do I changed that???). Anyway, the important thing is the auto generated <em>ValuesController</em> works. I now manually change the URL to point to the FilesController I created, i.e.</p>
  
  <p><em><a href="http://localhost:51932/api/files" rel="nofollow noreferrer">http://localhost:51932/api/files</a></em></p>
  
  <p>...and I get this:</p>
  
  <p><strong>HTTP 500 error
  That’s odd... the website can’t display this page</strong></p>
  
  <p>Now, if I remove the context argument from my <em>FilesController</em> - i.e. I go from:</p>
  
  <pre><code>public FilesController(PropWorxDbContext context)
          {
              _context = context;
          }
  </code></pre>
  
  <p>to </p>
  
  <pre><code>public FilesController()
          {
          }
  </code></pre>
  
  <p>then it works and it correctly displays:</p>
  
  <p><strong>this is the files controller</strong></p>
  
  <p>Any ideas why? Thanks, and sorry for the overly verbose post...</p>