EntityFrameworkCore.Tool MySql 数据库表自动生成对应类

.//0.安装:MySql
MySql.EntityFrameworkCore(5.05)
Microsoft.EntityFrameworkCore
//1.安装工具

  PM>>Install-Package Microsoft.EntityFrameworkCore.Tools   

https://docs.microsoft.com/en-us/ef/core/cli/powershell

//2.依据数据库生成类

PM>>Scaffold-DbContext "Server=172.16.88.89;port=3306;Database=testdb;uid=sa;pwd=123456;Character Set=utf8;" MySql.EntityFrameworkCore -OutputDir models2  

执行:自动生成数据库表对应的类
在这里插入图片描述

// 3.查看工具的帮助

PM>>Get-Help about_EntityFrameworkCore 
  --Add-Migration               Adds a new migration.
  --Drop-Database               Drops the database.
  --Get-DbContext               Gets information about a DbContext type.
  --Remove-Migration            Removes the last migration.
  --Scaffold-DbContext          Scaffolds a DbContext and entity types for a database.
  --Script-Migration            Generates a SQL script from migrations.
  --Update-Database             Updates the database to a specified migration.

应用

  //注册
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<Worker>();
                    //    services.AddDbContext<testdbContext>(options => options.UseMySQL("server=172.16.88.83;port=3306;database=dbtest;uid=sa;pwd=123456;CharSet=utf8"));
                    services.AddSingleton<Func<testdbContext>>(() =>
                    {
                        var optionsBuilder = new DbContextOptionsBuilder<testdbContext>();
                        optionsBuilder.UseMySQL("server=172.16.88.83;port=3306;database=dbtest;uid=sa;pwd=123456;CharSet=utf8");
                        return new testdbContext(optionsBuilder.Options);
                    });
                    services.AddSingleton(s =>
                    {
                        var optionsBuilder = new DbContextOptionsBuilder<testdbContext>();
                        optionsBuilder.UseMySQL("server=172.16.88.83;port=3306;database=dbtest;uid=sa;pwd=123456;CharSet=utf8");
                        return new testdbContext(optionsBuilder.Options);
                    });
                });
   
    //应用
    public class Worker : BackgroundService
    {
        private readonly ILogger<Worker> _logger;
        testdbContext _coreDbContext;

        public Worker(ILogger<Worker> logger, Func<testdbContext> dbContextFactory)
        {
            _logger = logger;
            _coreDbContext = dbContextFactory.Invoke();
        }
        public Worker(ILogger<Worker> logger, testdbContext context)
        {
            _logger = logger;
            testdbContext _coreDbContext = context;
        }
        public Worker(ILogger<Worker> logger, IServiceProvider _contextFactory)
        {
            _logger = logger;
            var context = _contextFactory.GetService(typeof(testdbContext));
        }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值