zoukankan      html  css  js  c++  java
  • Mysql EF Core 快速构建 Web Api

    (1)首先创建一个.net core web api web项目;

    (2)因为我们使用的是ef连接mysql数据库,通过NuGet安装MySql.Data.EntityFrameworkCore,以来的ef core 也会被安装.

    (2)在appsettings.json中添加如下数据库连接字符串.

    (2)创建数据库对应的model--AppUser

    public class AppUser
        {
            public int Id { get; set; }
    
            public string CompanyName { get; set; }
    
            public string Name { get; set; }
        }

    (3)创建dbcontext

    public class UserContext:DbContext
        {
            public UserContext(DbContextOptions<UserContext> options) : base(options)
            {
    
            }
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<AppUser>()
                    .ToTable("Users")
                    .HasKey(u => u.Id);
                base.OnModelCreating(modelBuilder);
    
            }
            public DbSet<AppUser> Users { get; set; }
        }

    (4)在startup.cs中配置数据库连接信息。并插入一条记录。

     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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    // 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.UseMvc();
                InitUserDatabase(app);
            }
            public void InitUserDatabase(IApplicationBuilder app)
            {
                using (var scope = app.ApplicationServices.CreateScope())
                {
                    var userContext = scope.ServiceProvider.GetRequiredService<UserContext>();
                    if (!userContext.Users.Any())
                    {
                        userContext.Users.Add(new Models.AppUser { Name = "daniel cui", CompanyName = "bat company" });
                        userContext.SaveChanges();
                    }
                }
            }

    (5)执行Add-Migration init生成Migrations文件夹,执行Update-Database init生成数据库并生成表.

    (6)启动程序并想Users表插入一条记录.

    但是会产生错误信息。目前还没有完美解决。

  • 相关阅读:
    linux ftp启用和停用及vsftpd安装 ssh 启用和停用命令及ssh的安装
    linux 网络命令
    printf的使用
    Shell echo命令
    shell脚本之文件操作
    shell脚本之字符串运算的使用
    linux的计划任务操作
    系统操作有关的命令
    shell脚本之 operater.sh 算术运算符;比较运算符;布尔运算符;逻辑与或非运算符;字符串运算符的使用
    shell脚本的参数传递使用
  • 原文地址:https://www.cnblogs.com/cby-love/p/11267472.html
Copyright © 2011-2022 走看看