zoukankan      html  css  js  c++  java
  • NetCore + Mysql CodeFirst 生成数据库

    首先定义领域的模型类,然后配置下面的一些东西,最后执行类

    1. 新建Context 继承自 DbContext 

    public class EFProjectContext : DbContext
        {
            public EFProjectContext(DbContextOptions<EFProjectContext> options) : base(options)
            {
    
            } 
    public DbSet<Address> Addreses { get; set; } public DbSet<Customer> Customers { get; set; } }

     2.在Startup类中获取mysql 连接字符串

      

     public void ConfigureServices(IServiceCollection services)
            {
                services.AddMvc();
    
                #region 获取数据库连接字符串 
                var connectionString = Configuration.GetConnectionString("DefaultConnection");
                //var builder = new ConfigurationBuilder();
                //builder.SetBasePath(Directory.GetCurrentDirectory());
                //builder.AddJsonFile("appsettings.json");
                //var connectionStringConfig = builder.Build();
                //var connectionString = connectionStringConfig["ConnectionStrings:DefaultConnection"];
                services.AddDbContext<EFProjectContext>(options => options.UseMySQL(connectionString));
                #endregion
    
                #region IOC
    
                services.AddTransient<IRepository<Customer>, Repository<Customer>>();
                services.AddTransient<ICustomerService, CustomerService>();
                #endregion
                #region AutoMapper
    
    
                #endregion
            }
    

      

      3. 在appsettings.json中配置 连接字符串

       

    "ConnectionStrings": {
        "DefaultConnection": "Server=localhost;database=netcore_test;uid=root;pwd=root;SslMode=None"
      }
    

     执行命令:  Add-Migration InitialCreate

           update-database 

  • 相关阅读:
    优秀个人博客
    redis 3.0 集群__监控警报工具(sentinel)
    redis 3.0 集群__hashTag
    shell__常用命令__sed
    shell__常用命令__grep
    shell__常用命令__awk
    shell 常用命令集合
    redis 3.0 集群__配置文件详解(常用配置)
    redis 3.0 集群__故障测评
    tcp 建立连接的三次握手,以及关闭连接的4次挥手
  • 原文地址:https://www.cnblogs.com/sunqiang/p/8399389.html
Copyright © 2011-2022 走看看