0
votes

I just installed Visual Studio 17 and I want to use mysql as my database to develop a WebAPI.

My csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="1.1.1" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />
  </ItemGroup>

</Project>

From NuGet package manager I installed Pomelo.EntityFrameworkCore.Mysql.

My aspsettings.json:

{
  "ConnectionStrings": {
    "MysqlConnection": "server=localhost;userid=root;pwd=root;port=3306;database=aspnet;sslmode=none;"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  }
}

In my strtup.cs inside ConfigureServices() I have:

services.AddDbContext<WebAPIDataContext>(options =>
            {
                options.UseMySQL(Configuration.GetConnectionString("MysqlConnection")); }
            );
 services.AddMvc();
 services.AddScoped<IProfileRepository, ProfileRepository>();

However, it is giving me DBContextOptionsBuilder does not contain a definition for UseMyQL error. Why is it that?

2

2 Answers

1
votes

I changed it to:

// Add framework services.
            services.AddDbContext<WebAPIDataContext>(options =>
            {
                options.UseMySql(Configuration.GetConnectionString("MysqlConnection"));
            });
0
votes

in Stertup.cs & appsettings.json & DbContext :

services.AddDbContext<mvccoreContext>(options =>
         options.UseMySql(Configuration.GetConnectionString("DefaultConnection")
        ));


     {
      "ConnectionStrings": {
        "DefaultConnection": "Server=localhost;Database=mvccore;User=root;Password=;"
      },
      "Logging": {
        "LogLevel": {
          "Default": "Warning"
        }
      },
      "AllowedHosts": "*"
    }


protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseMySql("");
        }
    }