zoukankan      html  css  js  c++  java
  • <五>.netcore webapi连接部署在docker中的mysql

    创建一个api,添加引用包

    Pomelo.EntityFrameworkCore.MySql
    Microsoft.EntityFrameworkCore.Design

    创建一个Data文件夹用来存放context,下一级创建一个Entity文件夹用来存放Entity

    创建一个UserContext

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

    创建一个Entity

      public class User
        {
            public int Id { get; set; }
            public string Name { get; set; }
            
            public string Company { get; set; }
            public string Title { get; set; }
        }

    startup中引入相关配置

     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.AddDbContext<UserContext>(options=>
                {
                    options.UseMySql(Configuration.GetConnectionString("MysqlUser"));
                });
    
                services.AddControllers();
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
    
                app.UseHttpsRedirection();
    
                app.UseRouting();
    
                app.UseAuthorization();
                InitializeDatabase(app);
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });
            }
    
    
            private void InitializeDatabase(IApplicationBuilder app)
            {
                using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
                {
                    var context = serviceScope.ServiceProvider.GetRequiredService<UserContext>();
                    context.Database.Migrate();
                    if (!context.Users.Any())
                    {
                        context.Users.Add(new Data.Entity.User() { Id = 1, Name = "test" });
                        context.SaveChanges();
                    }
                }
            }
        }

    appsetting中添加配置

     "ConnectionStrings": {
        "MysqlUser": "server=127.0.0.1;port=3306;database=DockerTest;userid=sa2;password=123456"
      }

    执行codefirst命令

    Add-Migration init
    Update-Database

    运行一下程序,查看下数据库

  • 相关阅读:
    VS2013 连接 MySQL
    2014年下半年的目标
    BI开发之——Mdx基础语法(2)(转至指尖流淌)
    BI开发之——Mdx基础语法(转至指尖流淌)
    数据仓库构建
    数据仓库的定义
    2014年计划:
    [转载]商业智能的三个层次
    BI入门基础知识-1
    ASP.NET MVC4 异常拦截
  • 原文地址:https://www.cnblogs.com/choii/p/14003098.html
Copyright © 2011-2022 走看看