asp.net core 缺少对UseMySQL的引用
最近在使用asp.net core的时候,数据库采用MySQL,出现了缺少对UseMySQL的引用。
具体代码如下:
Startup.csusing 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 SMS_ReciveDAL;
using Pomelo.EntityFrameworkCore.MySql;
using Microsoft.EntityFrameworkCore;
using MySql.Data.EntityFrameworkCore.Extensions;
namespace SMS_ReciveWeb
{
public class Startup
{
public Startup(IConfiguration configuration,IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile("config.json", optional: true, reloadOnChange: true);
Configuration = builder.Build();
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// 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(options =>
options.UseMySQL(
sqlConnectionString,
b => b.MigrationsAssembly("AspNetCoreMultipleProject")
)
);
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
出现这个错误的原因是:
缺少相应的引用。
解决方案:
增加对如下nuget包的引用,具体如下包:using Microsoft.EntityFrameworkCore;
using MySQL.Data.EntityFrameworkCore.Extensions;
我是在Visual Studio Code编辑器下开发的,关于如何添加nuget引用,可以参考下这篇文章:vscode 使用nuget
黄兵个人博客原创。