通过Mysql连接ASP.Net Core2.0(Code First模式)
ASP.NET Core2.0连接Mysql,首先新建项目

选择Web应用程序

选择需要身份验证:

通过Nuget安装Mysql驱动,这里推荐>Pomelo.EntityFrameworkCore.MySql(记得勾选预览版,目前只有此版本,虽然是预览,但是有rtm标识,所以放心就好)
具体使用细则,请参考:Pomelo.EntityFrameworkCore.MySql使用细则

增加Startup类中对Mysql的支持
把默认的options.UseSqlServer修改为options.UseMySql
public void ConfigureServices(IServiceCollection services)
{
//services.AddDbContext<ApplicationDbContext>(options =>
// options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddDbContext<ApplicationDbContext>(options =>
options.UseMySql(Configuration.GetConnectionString("MySqlConnection")));//Mysql支持 services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders(); services.AddMvc()
.AddRazorPagesOptions(options =>
{
options.Conventions.AuthorizeFolder("/Account/Manage");
options.Conventions.AuthorizePage("/Account/Logout");
}); // Register no-op EmailSender used by account confirmation and password reset during development
// For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=532713
services.AddSingleton<IEmailSender, EmailSender>();
}
Mysql实例信息:uid;root pwd:111111

打开appsettings.json文件,配置Mysql的连接字符串信息:
{
"ConnectionStrings": {
//"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-ASP.NETCore20ToMysql-53bc9b9d-9d6a-45d4-8429-2a2761773502;Trusted_Connection=True;MultipleActiveResultSets=true",
"MySqlConnection": "Server=localhost;database=ASP.NetCore-Mysql;uid=root;pwd=111111;"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}
运行网站,效果如下:

此时还未与数据库通信,需要触发如登录,注册等需要连接数据库的动作时,才会通过EF与数据库通信,并且发挥CodeFirst的作用,由应用程序的Model建立数据库的表结构,此处可点击Login后登录出现如下界面:

第一次运行会出现如下错误,是由于实体与数据库不统一,需要迁移所致,点击ApplyMigrations同意迁移后刷新网页即可
此时,打开Mysql数据库查看数据库是否新建成功

perfect,达到了预期效果
CodeFirst模式真是微软EF的一大利器,高效开发数据库应用必备哈
还有一种dbfirst模式:
执行以下命令创建实体:
Scaffold-DbContext "Data Source=.;Initial Catalog=Blogging;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models(Models是文件夹)
命令格式为:Scaffold-DbContext "Data Source=.;Initial Catalog=Blogging;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
通过Mysql连接ASP.Net Core2.0(Code First模式)的更多相关文章
- Asp.net Core2.0 缓存 MemoryCache 和 Redis
自从使用Asp.net Core2.0 以来,不停摸索,查阅资料,这方面的资料是真的少,因此,在前人的基础上,摸索出了Asp.net Core2.0 缓存 MemoryCache 和 Redis的用法 ...
- 【翻译】asp.net core2.0中的token认证
原文地址:https://developer.okta.com/blog/2018/03/23/token-authentication-aspnetcore-complete-guide token ...
- [翻译]在asp.net core2.0 OpenID Connect Handler中丢失了声明(CLaims)?
注:这是一篇翻译,来自这里.这篇文章讲述了在asp.net core2.0中使用openid connect handler的过程中解析不到你想要的claim时,你可以参考这篇文章. Missing ...
- 在阿里云Windows Server 上部署ASP .NET CORE2.0项目
近期使用ASP.NET Core2.0对博客进行了重写,在部署到服务器时遇到了一些问题,来记录一下留用. 配置环境 安装 .Net Framework3.5 在IIS管理器上直接开启,这里总是失败,上 ...
- 一步一步带你做WebApi迁移ASP.NET Core2.0
随着ASP.NET Core 2.0发布之后,原先运行在Windows IIS中的ASP.NET WebApi站点,就可以跨平台运行在Linux中.我们有必要先说一下ASP.NET Core. ASP ...
- 【转】Asp.Net Core2.0获取客户IP地址,及解决发布到Ubuntu服务器获取不到正确IP解决办法
1.获取客户端IP地址实现方法(扩展类) using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.ModelBinding; u ...
- Asp.Net Core2.0获取客户IP地址,及解决发布到Ubuntu服务器获取不到正确IP解决办法
1.获取客户端IP地址实现方法(扩展类) using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.ModelBinding; u ...
- VS2017创建一个 ASP.NET Core2.0 应用,并搭建 MVC 框架
https://testerhome.com/topics/11747 1.使用最新版本的VS2017,并安装.NET Core2.0中相关开发工具 2.打开VS2017,点击文件-新建-项目,选 ...
- WebApi迁移ASP.NET Core2.0
WebApi迁移ASP.NET Core2.0 一步一步带你做WebApi迁移ASP.NET Core2.0 随着ASP.NET Core 2.0发布之后,原先运行在Windows IIS中的AS ...
随机推荐
- 学习笔记:ALTERing a Huge MySQL Table - 对一个超大表做alter调整
Table of Contents The ProblemFuture SolutionsOverview of SolutionShortcutAssumptions/Restrictions/Co ...
- selenium-键盘和鼠标事件
常见的键盘操作 from selenium.webdriver.common.keys import Keys 模拟enter键:send_keys(Keys.ENTER)键盘F1~F12: send ...
- Go语言学习笔记(四)结构体struct & 接口Interface & 反射reflect
加 Golang学习 QQ群共同学习进步成家立业工作 ^-^ 群号:96933959 结构体struct struct 用来自定义复杂数据结构,可以包含多个字段(属性),可以嵌套: go中的struc ...
- 第 15 章 位操作(binbit)
/*------------------------------------ binbit.c -- 使用位操作显示二进制 ------------------------------------*/ ...
- October 22nd, 2017 Week 43rd Sunday
Yesterday is not ours to recover, but tomorrwo is ours to win or lose. 我们无法重拾昨天,但我们可以选择赢得或者输掉明天. Eve ...
- BZOJ5102:[POI2018]Prawnicy(贪心,堆)
Description 定义一个区间(l,r)的长度为r-l,空区间的长度为0. 给定数轴上n个区间,请选择其中恰好k个区间,使得交集的长度最大. Input 第一行包含两个正整数n,k(1<= ...
- PID控制本版二
https://gist.github.com/bradley219/5373998 特色: 比起第一版,加入了 最大最小值限制,暂无测试. PIDImpl( double dt, double ma ...
- ubuntu16.04忘记密码解决方案
主要解决通过sudo apt-get install安装mysql时输入密码时输错导致安装成功后,无法登陆MySQL. 之前遇到这种问题时,我个人比较喜欢通过卸载并重新安装解决,后来觉得这个虽然可以解 ...
- Grunt-jsdoc生成JS API文档
Grunt-jsdoc生成JS API文档 具体的请看官网 https://github.com/krampstudio/grunt-jsdoc 一:首先确保本机电脑上是否已经安装了nodejs和np ...
- HDU1232
https://vjudge.net/problem/HDU-1232 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都 ...