![](/img/trans.png)
[英]C# and Docker - Can't connect to containerized MySQL server from containerized .NET Core 3.1 Web API
[英]Can't Connect with MySql, .Net Core Api
我是新手,无法将 API 与数据库连接,我使用的是.Net Core 3
、 MySql Server 8.0.18
,还下载了 Connector/Net 8.0.18 和 Visual Studio 2019。这个是我的字符串连接:
"Server=localhost,3306;Database=portaldb;user=****;password=****"
我的 Startup.cs :
public void ConfigureServices(IServiceCollection services)
{
var connection = Configuration.GetConnectionString(Defaultconnection);
var mappingConfig = new MapperConfiguration(mc => {
mc.AddProfile(new MappingProfile());
});
var mapper = mappingConfig.CreateMapper();
services.AddControllers();
services.AddCors(options =>
{
options.AddPolicy(MyAllowSpecificOrigins,
builder =>
{
builder.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod();
});
});
services.AddSingleton(mapper);
services.AddTransient<IDbContext, PortalContext>();
services.AddTransient<ISqlRepository, SqlRepository>();
services.AddDbContext<PortalContext>(options => options.UseMySQL(connection));
}
我的数据库上下文:
public class PortalContext : DbContext, IDbContext
{
public PortalContext(DbContextOptions options) : base(options)
{
}
private DbSet<UsersModel> Users { get; set; }
private DbSet<CandidatesModel> Candidates { get; set; }
private DbSet<UserTypesModel> UserTypes { get; set; }
private DbSet<CompetenciesModel> Competencies { get; set; }
private DbSet<JobsModel> Jobs { get; set; }
private DbSet<SkillsAssessmentsModel> SkillsAssessments { get; set; }
private DbSet<SkillSetModel> Skills { get; set; }
///some methods
}
这是我的 DbContext 和我安装的软件包的使用的一些屏幕截图,我不确定这些是正确的还是我没有以正确的方式使用它们。
现在我收到这个错误
“内部连接致命错误。”
我尝试使用不同的连接字符串获取不同的错误,并尝试搜索答案,但几乎没有关于MySql
信息,很多关于Sql server
但没有关于MySql
。
编辑我更改了字符串连接和启动,但我仍然收到错误
: 'Method 'get_Info' in type 'MySql.Data.EntityFrameworkCore.Infraestructure.MySQLOptionsExtension' from assembly 'MySql.Data.EntityFrameworkCore, Version=8.0.18.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' does not have an implementation.'
编辑 2好的,我读到Microsoft.EntityFrameworkCore 3
它与MySql.Data.EntityFrameworkCore for Entity Framework - 8.0.18
不兼容MySql.Data.EntityFrameworkCore for Entity Framework - 8.0.18
所以我回滚到Microsoft.EntityFrameworkCore 2.1
,现在我Unable to connect to any of the specified MySQL hosts.
好的,正如 Panagiotis Kanavos 提到的,我使用了错误的连接字符串,因此我将其更改为:
Server=localhost;Database=portaldb;user=***;password=***
也正如东东所说,我正在使用options.UseSqlServer(connection)
而不是services.AddDbContext<PortalContext>(options => options.UseMySQL(connection));
有了这个,我只需要更改一些版本, Microsoft.EntityFrameworkCore 3
它与MySql.Data.EntityFrameworkCore for Entity Framework - 8.0.18
不兼容,我回滚到Microsoft.EntityFrameworkCore 2.1
,现在它可以工作了。
您的连接字符串在webconfig中应如下所示
<connectionStrings>
<add name="YourConnectionName" connectionString="Data Source=localhost;Port=3306(your DB port));Database=YourDBName;User Id=username;Password='password';SslMode=none" providerName="Mysql.Data.MysqlClient" />
</connectionStrings>
创建一个用于连接的类:
namespace Name_Component.DataAccess
{
public class DBConnection
{
public MySqlConnection FNName_MySqlConnection()
{
string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["YourConnectionName"].ConnectionString;
using (MySqlConnection mysqlConnection = new MySqlConnection(conStr))
{
return mysqlConnection;
}
}
}
}
然后像这样连接它:
using (MySqlConnection mysqlConnection = dbconnection.FNName_MySqlConnection())
{
...your action code for DB
}
这是在VS2017中。
您还需要添加Mysql dll作为参考
MySql.Data.EntityFrameworkCore 支持并且仅与 EF Core 2.1 兼容。 对于 EF Core 3.0 或最新版本,请使用 Pomelo.EntityFrameworkCore.MySql
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.