I am trying to create a web API in VS 2015 Pro (Update 3) using C # and targeting with .NET Core.
I follow this guide: https://docs.efproject.net/en/latest/platforms/aspnetcore/new-db.html#install-entity-framework . However, I am connecting to the MySQL database instead of the SQL Server database - I don't know what the difference is ...
In any case, in the tutorial, I should "register my context using dependency injection" - so I need to add the following line to the ConfigureServices Startup.cs file section :
var connection = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext< Models.PropWorxContext > (options => options.UseSqlServer(connection));;
However, VS gives me the following error:
Error CS1061 "DbContextOptionsBuilder" does not contain a definition for "UseSqlServer", and the extension method "UseSqlServer" that accepts the first argument of the type "DbContextOptionsBuilder" cannot be found (do you miss the using directive or assembly reference?)
Any ideas why?
Here's what the entire Startup.cs file looks like:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
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();
var connection = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext< Models.PropWorxContext > (options => options.UseSqlServer(connection));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
}
}
}
I also added the MySQL package using the package manager, so this project.json file contains this entry:
*"MySql.Data.EntityFrameworkCore": "7.0.6-IR31"*
Any hints about where I did wrong will be greatly appreciated, as I have been trying to figure this out all day :( Thanks ...