core3.1mysql_Core3.1连接MySql
1. 创建新项⽬-ASP.NET Core Web 应⽤程序
2.
3. 右键项⽬-管理 NuGet 程序包(N)...
4. 搜索  Pomelo.EntityFrameworkCore.MySql  安装
5. 在appsettings.json⽂件添加 数据库连接字符串
"AllowedHosts": "*","ConnectionStrings": {"MysqlConnection": "Data Source=192.168.199.999;Database=gf;User ID=root;Password=123456;pooling=true;port=3306;sslmode=none;CharSet=utf8;"}
6. 添加⼀个Model
[Table("flash_map2")] //特性,标记为mySql数据库中的具体表名
public class MapFlash
{
[Key] //特性,标记为主键
public int id_no { get; set; }
public string flash_name { get; set; }
}
7. 添加MysqlDbContext类,⽤来连接数据库
public classMysqlDbContext: DbContext
{public MysqlDbContext(DbContextOptions options) : base(options)
{
}public DbSet flash_map23 { get; set; }
}
8.  在Startup类的ConfigureServices的⽅法中注⼊⼀下
public voidConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddMvc();
services.AddScoped();var connection = Configuration.GetConnectionString("MysqlConnection");
services.AddDbContext(options =>options.UseMySql(connection));
}
9. 到这⼀步就完成全部配置了,然后可以使⽤了
10. 创建⼀个控制器,使⽤⼀下
public classTestController : Controller
{privateDbContext dbContext;publicTestController(DbContext _dbContext)
{this.dbContext =_dbContext;
}publicIActionResult Index()
{var data = dbContext.Set().Find(37); ViewData["aa"] =data.flash_name;returnView(); }
}
11. 完成
mysql下载链接12. 如有其他疑问请在评论区留⾔
13. ⼤佬请直接略过

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。