lidd~ 2022-06-01 00:00 采纳率: 55.6%
浏览 71
已结题

ASP.NET Core5.0 MVC中 Pomelo.EntityFrameworkCore.MySql包在框架net5.0怎么连接数据库

ASP.NET Core5.0 MVC中 Pomelo.EntityFrameworkCore.MySql包在框架net5.0怎么连接数据库

appsettings.json

img

Startup.cs

img

里面的代码怎么写才能连接mysql

  • 写回答

2条回答 默认 最新

  • Rena Ni 2022-06-01 10:19
    关注

    首先不论你使用什么支持MYSQL的nuget,你要先安装Microsoft.EntityFrameworkCore version 5.0.17。
    创建好对应的DbContext, 例如:

    public class MyDBContext : DbContext
    {
        public MyDBContext(DbContextOptions<MyDBContext> options) : base(options)
        {
        }
        public DbSet<User> Users { get; set; }
    }
    

    然后安装MySql.EntityFrameworkCore version 5.0.13。 参考:

    更改你的Startup.cs代码:

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            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)
        {
            services.AddControllersWithViews();
            services.AddDbContext<MyDBContext>(opt => opt.UseMySQL(Configuration.GetConnectionString("WebApplicationContext")));
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
  • Null_Reference 2022-06-01 09:11
    关注
    
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            // Replace with your connection string.
            var connectionString = "server=localhost;user=root;password=1234;database=ef";
    
            // Replace with your server version and type.
            // Use 'MariaDbServerVersion' for MariaDB.
            // Alternatively, use 'ServerVersion.AutoDetect(connectionString)'.
            // For common usages, see pull request #1233.
            var serverVersion = new MySqlServerVersion(new Version(8, 0, 27));
    
            // Replace 'YourDbContext' with the name of your own DbContext derived class.
            services.AddDbContext<YourDbContext>(
                dbContextOptions => dbContextOptions
                    .UseMySql(connectionString, serverVersion)
                    // The following three options help with debugging, but should
                    // be changed or removed for production.
                    .LogTo(Console.WriteLine, LogLevel.Information)
                    .EnableSensitiveDataLogging()
                    .EnableDetailedErrors()
            );
        }
    }
    
    

    开源地址
    https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql

    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月9日
  • 已采纳回答 6月1日
  • 创建了问题 6月1日

悬赏问题

  • ¥50 求!AutomationDesk 如何自动导入Variant数据
  • ¥20 Next.JS 静态导出路由
  • ¥15 我做一个对中文文本情感分析的项目 我用了CNN,和keras框架 报的错误我一直处理不好
  • ¥15 unity使用bakery打光烘焙所遇到的问题。
  • ¥99 二维有限元方法求解,泊松方程
  • ¥15 我需要在PC端 开两个抖店工作台客户端.(语言-java)
  • ¥15 有没有哪位厉害的人可以用C#可视化呀
  • ¥15 可以帮我看看代码哪里错了吗
  • ¥15 设计一个成绩管理系统
  • ¥15 PCL注册的选点等函数如何取消注册