【问题标题】:Pomelo.EntityFrameworkCore.Mysql error DBContextOptionsBuilder does not contain a definition for UseMyQLPomelo.EntityFrameworkCore.Mysql 错误 DBContextOptionsBuilder 不包含 UseMyQL 的定义
【发布时间】:2017-09-30 04:36:36
【问题描述】:

我刚刚安装了 Visual Studio 17,我想使用 mysql 作为我的数据库来开发 WebAPI。

我的 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>

我从 NuGet 包管理器安装了 Pomelo.EntityFrameworkCore.Mysql。

我的 aspsettings.json:

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

在 ConfigureServices() 内的 strtup.cs 中,我有:

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

但是,它给了我DBContextOptionsBuilder does not contain a definition for UseMyQL 错误。为什么会这样?

【问题讨论】:

    标签: asp.net-core entity-framework-core visual-studio-2017


    【解决方案1】:

    我改成:

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

    【讨论】:

      【解决方案2】:

      在 Stetup.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("");
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2018-04-11
        • 1970-01-01
        • 2017-11-25
        • 2017-03-13
        • 2021-08-03
        • 2021-12-17
        • 1970-01-01
        • 2017-08-05
        • 2018-12-25
        相关资源
        最近更新 更多