gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:16:04 26 4
gpt4 key购买 nike

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

这是我的错误:

Severity Code Description Project File Line Suppression StateError CS1061 'DbContextOptionsBuilder' does not contain a definitionfor 'UseMySql' and no extension method 'UseMySql' accepting a firstargument of type 'DbContextOptionsBuilder' could be found (are youmissing a using directive or an assemblyreference?) LearnEFCore d:\temp\aspnet\LearnEFCore\LearnEFCore\Startup.cs 29 Active

我的代码如下:

Startup.cs

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>

最佳答案

更改为 Pomelo.EntityFrameworkCore.MySql 解决了问题...

关于mysql - 错误 CS1061 'DbContextOptionsBuilder' 不包含 'UseMySql' 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47015809/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com