C# ASP.NET.Core Web API可以';无法连接到MariaDB数据库

C# ASP.NET.Core Web API可以';无法连接到MariaDB数据库,c#,entity-framework-core,mariadb,pomelo-entityframeworkcore-mysql,C#,Entity Framework Core,Mariadb,Pomelo Entityframeworkcore Mysql,我目前正在开发一个ASP.NET核心Web API,它应该可以在MariaDB 10.4数据库上运行。到目前为止,无论何时通过Visual Studio 2019启动IIS服务器,都不会引发任何错误或警告。通过使用EF核心框架和Pomelo.Mysl库实现与上述数据库的连接。相关文件如下所示: Startup.cs: Program.cs: 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用System.Threading.Tasks; 使

我目前正在开发一个ASP.NET核心Web API,它应该可以在MariaDB 10.4数据库上运行。到目前为止,无论何时通过Visual Studio 2019启动IIS服务器,都不会引发任何错误或警告。通过使用EF核心框架和Pomelo.Mysl库实现与上述数据库的连接。相关文件如下所示:

Startup.cs:

Program.cs:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.EntityFrameworkCore.Internal;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用Microsoft.Extensions.Hosting;
使用Microsoft.Extensions.Logging;
使用racoon.Data;
名称空间浣熊
{
公共课程
{
公共静态void Main(字符串[]args)
{
var host=CreateHostBuilder(args.Build();
host.Run();
}
私有静态void CreateDbIfNotExists(IHost主机)
{
使用(var scope=host.Services.CreateScope())
{
var services=scope.ServiceProvider;
尝试
{
var context=services.GetRequiredService();
context.Database.recreated();
}
捕获(例外情况除外)
{
var logger=services.GetRequiredService();
logger.LogError(例如,“创建数据库时出错”);
}
}
}
公共静态IHostBuilder CreateHostBuilder(字符串[]args)=>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder=>
{
webBuilder.UseStartup();
});
}
}
使用中的选项说明您正在连接到MariaDB,而不是默认的Mysql数据库

services.AddDbContext<CollabradorContext>(options => options      
                .UseMySql("Server=localhost;Database=db;User=usr;Password=foo;",      
                    mysqlOptions =>      
                        mysqlOptions.ServerVersion(new ServerVersion(new Version(10, 4, 6), ServerType.MariaDb))));
services.AddDbContext(选项=>options
.UseMySql(“Server=localhost;Database=db;User=usr;Password=foo;”,
mysqlOptions=>
ServerVersion(新的ServerVersion(新的版本(10,4,6),ServerType.MariaDb));

Man首先,你不应该把你的密码和用户名写在public上。其次,最简单的例子不是你发布的大量代码。根据网站规则投票关闭-提供一个简单的例子。确保你没有在本地以外的任何地方使用该密码。它现在永远存在(例如,它可以在帖子的历史中看到)。这不应该是你问题的答案。诚然,您应该始终使用
ServerVersion
选项来指定您使用的是哪个版本的MySQL或MariaDB,但在建立到数据库的连接时,不指定此选项应该不会产生任何影响。
ServerVersion
选项仅在生成SQL查询时才被查询,甚至仅在特定情况下才被查询。因此,您的问题可能与fireall/port等有关。抛出的确切异常(包括堆栈跟踪)是什么?(或者如果没有抛出,那么问题是什么?)
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using racoon.Models;
using System.Net.Mime;

namespace racoon.Data
   {
public class SampleContext : DbContext
{
    public SampleContext(DbContextOptions<SampleContext> options) : base(options) {}

    public DbSet<Course> Courses { get;set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Course>().ToTable("Course");
    }
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace racoon.Models
    {
    public class Course
    {
        public long Id;
        public string Name;
        public string Description;
        public int Rating;
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using racoon.Models;
using System.Threading.Tasks;

namespace racoon.Data
{
    public static class DbInitalizer
{
    public static void Initalize(CollabradorContext context)
    {
        System.Console.WriteLine("Initalize!");
        context.Database.EnsureCreated();

        if (context.Students.Any())
        {
            return;
        }

        var courses = new Course[]
        {
            new Course{Id=0,Name="Python",Description="Learn the fundamentals of Python",Rating=0},
            new Course{Id=1,Name="ML",Description="Learn everything about Machine Learning",Rating=0}
        };
        foreach (Course c in courses)
        {
            context.Courses.Add(c);
        }
        context.SaveChanges();
    }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using racoon.Data;

namespace racoon
{
    public class Program
{
    public static void Main(string[] args)
    {
        var host = CreateHostBuilder(args).Build();
        host.Run();
    }

    private static void CreateDbIfNotExists(IHost host)
    {
        using (var scope = host.Services.CreateScope())
        {
            var services = scope.ServiceProvider;

            try
            {
                var context = services.GetRequiredService<SampleContext>();
                context.Database.EnsureCreated();
            }
            catch (Exception ex)
            {
                var logger = services.GetRequiredService<ILogger<Program>>();
                logger.LogError(ex, "An error occurred creating the DB.");
            }
        }
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}
}
services.AddDbContext<CollabradorContext>(options => options      
                .UseMySql("Server=localhost;Database=db;User=usr;Password=foo;",      
                    mysqlOptions =>      
                        mysqlOptions.ServerVersion(new ServerVersion(new Version(10, 4, 6), ServerType.MariaDb))));