Ho appena imparato a utilizzare ASP.NET CORE 2.0 MVC utilizzando Visual Studio Community Edition. Voglio usare il database MySQL invece di usare SQL Server perché ho bisogno di usare alcuni dati all'interno del vecchio DB MySQL. Per favore aiutami a risolvere questo problema .. grazie
Ecco il mio errore:
Codice di Severità Descrizione Errore di stato della riga di eliminazione del file di progetto CS1061 'DbContextOptionsBuilder' non contiene una definizione per 'UseMySql' e non è possibile trovare il metodo di estensione 'UseMySql' che accetta un primo argomento di tipo 'DbContextOptionsBuilder' (manca una direttiva using o un riferimento all'assembly?) LearnEFCore d: \ temp \ aspnet \ LearnEFCore \ LearnEFCore \ Startup.cs 29 Active
Il mio codice come segue:
In 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();
}
Nel mio appettings.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionStrings": {
"DataAccessMySqlProvider": "server=localhost;port=3306;userid=root;password=root;database=sportstore;"
}
}
Nei miei modelli / dati
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");
}
}
}
Il mio 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>
changin to Pomelo.EntityFrameworkCore.MySql ha risolto il problema ...