webapi mysql_.Net Core 的WebApi项目使用mysql的EF CodeFirst模式

1.建立.net core的webapi项目参看:

2.在项目中添加MyDBContext类和实体类User

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

namespace TestCore.Model

{

public class User

{

public int ID { get; set; }

public string Name { get; set; }

public string Email { get; set; }

public string Bio { get; set; }

}

}

using Microsoft.EntityFrameworkCore;

using Microsoft.Extensions.Configuration;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using TestCore.Model;

namespace TestCore

{

public class MyDBContext : DbContext

{

public MyDBContext(DbContextOptions options)

: base(options)

{

}

public DbSet User { get; set; }

}

}

3.在Startup类中的ConfigureServices方法添加如下代码:

// This method gets called by the runtime. Use this method to add services to the container.

public void ConfigureServices(IServiceCollection services)

{

var connectionString = Configuration["ConnectionStrings:DefaultConnection"];

services.AddDbContext(options =>

options.UseMySQL(connectionString)

);

services.AddMvc();

}

appsetting.json中添加:

"ConnectionStrings": {

"DefaultConnection": "server=localhost;user id=root;pwd=root;database=testcore;"

},

还有一种写法是在MyDBContext中配置链接字符串,好处是可以直接用new MyDBContext();初始化:

Startup类中:

public static IConfiguration Configuration { get; private set; }

MyDBContext类中:

public class MyDBContext : DbContext

{

//public MyDBContext(DbContextOptions options)

// : base(options)

//{

//}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

{

var connectionString = Startup.Configuration["ConnectionStrings:DefaultConnection"];

optionsBuilder.UseMySQL(connectionString);

}

public DbSet User { get; set; }

public DbSet Article { get; set; }

}4.控制台内执行

Add-Migration xxx

Update-Database

5.使用swagger

添加引用:Swashbuckle.AspNetCore

public void ConfigureServices(IServiceCollection services)

{

//使用mysql

var connectionString = Configuration["ConnectionStrings:DefaultConnection"];

services.AddDbContext(options =>

options.UseMySQL(connectionString)

);

//Init Swagger

services.AddSwaggerGen(options =>

{

options.SwaggerDoc("v1", new Info

{

Version = "v1",

Title = "WebAPI"

});

//Determine base path for the application.

var basePath = PlatformServices.Default.Application.ApplicationBasePath;

//Set the comments path for the swagger json and ui.

var xmlPath = Path.Combine(basePath, "TestCore.xml");//TestCore.xml是项目帮助文档,需要在项目属性-生成里开启文档

options.IncludeXmlComments(xmlPath);

});

services.AddMvc();

}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)

{

if (env.IsDevelopment())

{

app.UseDeveloperExceptionPage();

}

//InitSwagger

app.UseSwagger();

app.UseSwaggerUI(c =>

{

c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebAPI V1");

c.ShowRequestHeaders();

});

app.UseMvc();

}

6.发布到Linux环境,参考文章:

http://www.voidcn.com/article/p-ecxigwsg-brs.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值