我刚刚安装了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"
}
}
}
在我的配置服务()中的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
错误。为什么会这样?
答案 0 :(得分:0)
我把它改为:
// Add framework services.
services.AddDbContext<WebAPIDataContext>(options =>
{
options.UseMySql(Configuration.GetConnectionString("MysqlConnection"));
});
答案 1 :(得分:0)
在Stertup.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("");
}
}