You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Visual Studio for Mac中用.NET 7 EF Core连接MySQL 8.0.31遇阻

.NET 7 EF Core 连接 MySQL 8.0.31 数据库问题求助

我已花费数日研究并尝试多种方法,但在.NET 7 EF Core中连接MySQL 8.0.31数据库始终没有一套连贯可行的方案。数据库通过MySQL Workbench创建和托管,无论采用哪种解决方案、教程,总有部分环节无法正常运行。

我正在MacOS的Visual Studio中使用C#开发一款联系人管理Web应用,当前的Program.cs代码如下:

using System.Xml;
using Microsoft.EntityFrameworkCore;

namespace CMProject;

public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        // Add services to the container.
        var serverVersion = new MySqlServerVersion(new Version(8, 0, 31));
        string connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
        builder.Services.AddDbContext<ContactManagerContext>(options => options.UseMySql(connectionString, serverVersion));

        builder.Services.AddRazorPages();

        var app = builder.Build();

        // Configure the HTTP request pipeline.
        if (!app.Environment.IsDevelopment())
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

        app.MapRazorPages();

        app.Run();
    }
}

ContactManagerContext代码如下:

using System;
using Microsoft.EntityFrameworkCore;

namespace CMProject
{
    public class ContactManagerContext : DbContext
    {

        public ContactManagerContext(DbContextOptions<ContactManagerContext> options) : base(options)
        {
        }

        public DbSet<Contact> Contacts { get; set; }
    }
}

在部分教程中,讲师通过在Main方法中直接实例化ContactManagerContext类(如ContactContextManager context = new ContactContextManager())来访问数据,视频中可以正常运行,但我这边却报错:There is no argument given that corresponds to the required parameter 'options' of....,这类问题层出不穷。

我尝试了多种方法,仍不清楚问题所在,恳请各位提供思路和建议。

内容的提问来源于stack exchange,提问作者Syneptic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 18:45:14