错误CS1061'DbContextOptionsBuilder'不包含'UseMySql'的定义

问题描述 投票:0回答:1

我刚学会使用Visual Studio Community Edition使用ASP.NET CORE 2.0 MVC。我想使用MySQL数据库而不是使用SQL Server,因为我需要在旧MySQL数据库中使用一些数据。请帮我解决这个问题..谢谢

这是我的错误:

严重级代码描述项目文件行抑制状态错误CS1061'DbContextOptionsBuilder'不包含'UseMySql'的定义,并且没有可以找到接受类型'DbContextOptionsBuilder'的第一个参数的扩展方法'UseMySql'(您是否缺少using指令或程序集引用?)LearnEFCore d:\ temp \ aspnet \ LearnEFCore \ LearnEFCore \ Startup.cs 29活动

我的代码如下:

在Startup.cs(https://pastebin.com/dzx8Hf8Q

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using LearnEFCore.Data;

....
// This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var sqlConnectionString = Configuration.GetConnectionString("DataAccessMySqlProvider");
            services.AddDbContext<SchoolContext>(options => options.UseMySql(sqlConnectionString));
            services.AddMvc();
        }

在我的appsettings.json中

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },

  "ConnectionStrings": {
    "DataAccessMySqlProvider": "server=localhost;port=3306;userid=root;password=root;database=sportstore;"
  }
}

在我的模型/数据中

using LearnEFCore.Models;
using Microsoft.EntityFrameworkCore;

namespace LearnEFCore.Data
{
    public class SchoolContext : DbContext
    {
        public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
        {
        }

        public DbSet<Course> Courses { get; set; }
        public DbSet<Enrollment> Enrollments { get; set; }
        public DbSet<Student> Students { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Course>().ToTable("Course");
            modelBuilder.Entity<Enrollment>().ToTable("Enrollment");
            modelBuilder.Entity<Student>().ToTable("Student");
        }
    }
}

我的csproj

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

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" />
    <PackageReference Include="MySql.Data.EntityFrameworkCore" Version="6.10.4" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
  </ItemGroup>

</Project>
mysql asp.net entity-framework-core dbcontext asp.net-core-2.0
1个回答
0
投票

改为Pomelo.EntityFrameworkCore.MySql解决了这个问题......

© www.soinside.com 2019 - 2024. All rights reserved.