Asp.Net Core 连接Mysql
上一篇文章里最后在VS里测试是没有问题的,但是在Windows命令行模式下会报错。
首先用dotnet restore命令的时候会出现error:
然后用dotnet run会出现警告,但是依旧会成功启动:
此时访问http://localhost:5000/user/query/2 时会报错:
其实在VS中也是有提示的:
只不过在VS中调试是不会报错的,问题就出在依赖项上,可以看看project.json这个文件,所有的依赖项都在这里:
{ "dependencies": { "Microsoft.NETCore.App": { "version": "1.0.0-rc2-3002702", "type": "platform" }, "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final", "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final", "Microsoft.AspNetCore.Razor.Tools": { "version": "1.0.0-preview1-final", "type": "build" }, "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final", "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final", "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final", "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final", "Microsoft.Extensions.Logging": "1.0.0-rc2-final", "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final", "Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final", "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc2-final", "MySql.Data": "7.0.6-IR31", "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final", "Dapper": "1.50.2" }, "tools": { "Microsoft.AspNetCore.Razor.Tools": { "version": "1.0.0-preview1-final", "imports": "portable-net45+win8+dnxcore50" }, "Microsoft.AspNetCore.Server.IISIntegration.Tools": { "version": "1.0.0-preview1-final", "imports": "portable-net45+win8+dnxcore50" } }, "frameworks": { "netcoreapp1.0": { "imports": [ "dotnet5.6", "dnxcore50", "portable-net45+win8" ] } }, "buildOptions": { "emitEntryPoint": true, "preserveCompilationContext": true }, "runtimeOptions": { "gcServer": true }, "publishOptions": { "include": [ "wwwroot", "Views", "appsettings.json", "web.config" ] }, "scripts": { "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ], "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ] } }
那就把Microsoft.Extensions.Configuration.Json这个替换成新的吧,可以在
http://www.nuget.org/packages/Microsoft.Extensions.Configuration.Json/ 上查看版本信息,或者在VS中用包管理器选择最新的
更方便的方法是直接编辑project.json文件,会有自动提示:
选择最新包后在VS里重新生成是不会加载更新包的,需要在文件目录下使用dotnet restore命令来重新下载包,restore的时候还是会报错:
提示Mysql.Data 7.0.6-IR31与 .NETCoreApp,Version=v1.0 不兼容,此时用dotnet run运行直接就终止程序了:
此时单独改Microsoft.Extensions.Configuration还是会报错,那我直接把Microsoft.NETCore.App改成最新的:
"Microsoft.NETCore.App": { "version": "1.1.0-preview1-001100-00", "type": "platform" },
单独改这个估计其他的也得改,干脆一起都改了:
{ "dependencies": { "Microsoft.NETCore.App": { "version": "1.1.0-preview1-001100-00", "type": "platform" }, "Microsoft.AspNetCore.Diagnostics": "1.1.0-preview1-final", "Microsoft.AspNetCore.Mvc": "1.1.0-preview1-final", "Microsoft.AspNetCore.Razor.Tools": { "version": "1.0.0-preview1-final", "type": "build" }, "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0-preview1-final", "Microsoft.AspNetCore.Server.Kestrel": "1.1.0-preview1-final", "Microsoft.AspNetCore.StaticFiles": "1.1.0-preview1-final", "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0-preview1-final", "Microsoft.Extensions.Logging": "1.1.0-preview1-final", "Microsoft.Extensions.Logging.Console": "1.1.0-preview1-final", "Microsoft.Extensions.Logging.Debug": "1.1.0-preview1-final", "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.1.0-preview1-final", "MySql.Data": "7.0.6-IR31", "Microsoft.Extensions.Configuration.Json": "1.1.0-preview1-final", "Dapper": "1.50.2" }, "tools": { "Microsoft.AspNetCore.Razor.Tools": { "version": "1.0.0-preview1-final", "imports": "portable-net45+win8+dnxcore50" }, "Microsoft.AspNetCore.Server.IISIntegration.Tools": { "version": "1.0.0-preview1-final", "imports": "portable-net45+win8+dnxcore50" } }, "frameworks": { "netcoreapp1.0": { "imports": [ "dotnet5.6", "dnxcore50", "portable-net45+win8" ] } }, "buildOptions": { "emitEntryPoint": true, "preserveCompilationContext": true }, "runtimeOptions": { "gcServer": true }, "publishOptions": { "include": [ "wwwroot", "Views", "appsettings.json", "web.config" ] }, "scripts": { "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ], "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ] } }
resotre的时候依然会提示不兼容,但此时dotnet run可以正常运行了:
在浏览器输入http://localhost:5000/user/query/1 也能正常出结果了。
这里restore报错的问题我想可能是VS版本造成的,我这个项目是在VS 2015 update2 下创建的,我把VS2015升级到Update3后重新创建项目就没有这个错误。
接下来把Asp.Net Core程序发布到CentOS上测试:
使用XFtp把src目录下的文件夹上传到CentOS上,在CentOS里cd到程序目录下,运行dotnet restore,完成后运行dotnet run,发现报错了:
这个时候需要把复制过去的project.lock.json文件删除,然后再运行dotnet restore,完成后运行dotnet run,编译通过:
因为CentOS上的dotnet版本不是最新的,更新下成最新的,到
https://github.com/dotnet/core/ 上找到最新版本
wget https://dotnetcli.blob.core.windows.net/dotnet/release/1.1.0/Binaries/Latest/dotnet-centos-x64.latest.tar.gz tar zxf dotnet.tar.gz -C /opt/dotnet
重新运行dotnet run:
还是不行,这就郁闷了,那再把版本改低点吧:
{ "dependencies": { "Microsoft.NETCore.App": { "version": "1.0.1", "type": "platform" }, "Microsoft.AspNetCore.Diagnostics": "1.0.0", "Microsoft.AspNetCore.Mvc": "1.0.1", "Microsoft.AspNetCore.Razor.Tools": { "version": "1.0.0-preview1-final", "type": "build" }, "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0", "Microsoft.AspNetCore.Server.Kestrel": "1.0.1", "Microsoft.AspNetCore.StaticFiles": "1.0.0", "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0", "Microsoft.Extensions.Logging": "1.0.0", "Microsoft.Extensions.Logging.Console": "1.0.0", "Microsoft.Extensions.Logging.Debug": "1.0.0", "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.1.0-preview1-final", "MySql.Data": "7.0.6-IR31", "Microsoft.Extensions.Configuration.Json": "1.0.0", "Dapper": "1.50.2" }, "tools": { "Microsoft.AspNetCore.Razor.Tools": { "version": "1.0.0-preview1-final", "imports": "portable-net45+win8+dnxcore50" }, "Microsoft.AspNetCore.Server.IISIntegration.Tools": { "version": "1.0.0-preview1-final", "imports": "portable-net45+win8+dnxcore50" } }, "frameworks": { "netcoreapp1.0": { "imports": [ "dotnet5.6", "dnxcore50", "portable-net45+win8" ] } }, "buildOptions": { "emitEntryPoint": true, "preserveCompilationContext": true }, "runtimeOptions": { "gcServer": true }, "publishOptions": { "include": [ "wwwroot", "Views", "appsettings.json", "web.config" ] }, "scripts": { "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ], "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ] } }
重新resotre run,正常了,在终端输入:
curl http://localhost:5000
可以看到首页的源码了。
再输入curl http://localhost:5000/user/getname/1:
终于成功了!这里的地址我是用的另外一个项目,上一篇文章的项目地址应该是:http://localhost:5000/user/query/1
把现在项目的源码也放上来吧,除了Controller外没别的变化:
public class UserController : Controller { private static MySql.Data.MySqlClient.MySqlConnection GetMySqlConnection(bool open = true, bool convertZeroDatetime = false, bool allowZeroDatetime = false) { string cs = "Server=10.255.19.111;Database=mydb;Uid=root;Pwd=yourpassword;"; var csb = new MySql.Data.MySqlClient.MySqlConnectionStringBuilder(cs); csb.AllowZeroDateTime = allowZeroDatetime; csb.ConvertZeroDateTime = convertZeroDatetime; var conn = new MySql.Data.MySqlClient.MySqlConnection(csb.ConnectionString); if (open) conn.Open(); return conn; } // GET: /<controller>/ public IActionResult Index() { return View(); } public IActionResult GetName(int id) { string name = ""; using (var connection = GetMySqlConnection()) { var user = connection.QueryFirstOrDefault<User>("select * from User where id=@Id;", new { Id = id }); if (user != null) name = user.Name; return Content(name); } } }
下面需要从外部访问了,可以参考我之前的帖子:
使用.Net Core发布可从外部访问的网站
当然需要先把5000端口打开:
firewall-cmd --zone=/tcp --permanent firewall-cmd --reload
在Program的Main方法中加入:
.UseKestrel().UseUrls("http://*:5000")
重启程序dotnet restore dotnet run
在Windows下访问:http://10.255.19.111:5000/
能看到首页了,此时可以看到CentOS里面的运行状态:
再打开http://10.255.19.111:5000/user/getname/2
至此就算圆满完成了!
Asp.Net Core 连接Mysql的更多相关文章
- 处理asp.net core连接mysql的一个异常Sequence contains more than one matching element
晚上在那里调程序,把mysql.data组件的nuget包进行了更新,前几天好好的程序,开始抛错,跟踪断点发现以下的异常: Unable to connect to any of the specif ...
- Visual Studio Code和Docker开发asp.net core和mysql应用
Visual Studio Code和Docker开发asp.net core和mysql应用 .net猿遇到了小鲸鱼,觉得越来越兴奋.本来.net猿只是在透过家里那田子窗看外面的世界,但是看着海峡对 ...
- 第八节:EF Core连接MySql数据库
一. 前提 1.安装EF Core连接MySQL的驱动,这里有两类: (1).Oracle官方出品:MySql.Data.EntityFrameworkCore (版本:8.0.17) (2).其他第 ...
- ASP.NET Core MVC+Layui使用EF Core连接MySQL执行简单的CRUD操作
前言: 本章主要通过一个完整的示例讲解ASP.NET Core MVC+EF Core对MySQL数据库进行简单的CRUD操作,希望能够为刚入门.NET Core的小伙伴们提供一个完整的参考实例.关于 ...
- docker4dotnet #3 在macOS上使用Visual Studio Code和Docker开发asp.net core和mysql应用
.net猿遇到了小鲸鱼,觉得越来越兴奋.本来.net猿只是在透过家里那田子窗看外面的世界,但是看着海峡对岸的苹果园越来越茂盛,实在不想再去做一只宅猿了.于是,.net猿决定搭上小鲸鱼的渡轮到苹果园去看 ...
- asp.net core容器&mysql容器network互联 & docker compose方式编排启动多个容器
文章简介 asp.net core webapi容器与Mysql容器互联(network方式) docker compose方式编排启动多个容器 asp.net core webapi容器与Mysql ...
- Asp.Net Core链接Mysql数据库
一.新建一个Asp.Net Core WebMVC程序 添加nuget包 Mysql.Data 二.新建一个UserContext类 下面代码中的UserInfo是我自己建的一个实体,里面有俩字段: ...
- ASP.Net MVC连接MySQL和Code First的使用
首先要准备一下的工具作为环境 MySQL Community Server 5.7.x My Workbench 6.3 VS2017 新建一个项目,NetMySQLCodeFirst 选择MVC,再 ...
- asp.net mvc4连接mysql
环境:vs2013+mysql5.6+mysql connector for .net 6.8.3+MySQL for Visual Studio 1.1.3 参考:http://dev.mysql. ...
随机推荐
- 浅谈webuploader上传文件
官网:http://c7.gg/fw4sn 案例: 文件上传进度 // 文件上传过程中创建进度条实时显示. uploader.on( 'uploadProgress', function( file, ...
- 5) mvn archetype:generate
获取帮助 mvn -h 命令格式 usage: mvn [options] [<goal(s)>] [<phase(s)>] -D,--define <arg> D ...
- ENVI数据格式
选择一个或多个感兴趣的图层: 分类:目视解译.非监督分类 投影 哪里需要七参数,没有必要七参数吧?如果精度要求不高的话
- (最小生成树) 畅通工程再续 -- HDU --1875
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1875 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- 基于Verilog HDL的二进制转BCD码实现
在项目设计中,经常需要显示一些数值,比如温湿度,时间等等.在数字电路中数据都是用二进制的形式存储,要想显示就需要进行转换,对于一个两位的数值,对10取除可以得到其十位的数值,对10取余可以得到个位的数 ...
- Socket常用语法与socketserver实例
1>Socket相关: 1>Socket Families(地址簇): socket.AF_UNIX 本机进程间通信 socket.AF_INET IPV4 socket.AF_I ...
- LeetCode137:Single Number II
题目: Given an array of integers, every element appears three times except for one. Find that single o ...
- Chrome自定义缩放百分比
我想要设置Chrome页面缩放为120%,但是Chrome只提供110% 125%,根本没有让我舒心的缩放比例. 强迫症发作,谷歌了半天没有一个很好的解决方案. 虽然也有不少第三方扩展可以自定义缩放比 ...
- NLayerAppV3--DDD之领域层
回顾:NLayerAppV3是一个使用.net 2.1实现的经典DDD的分层架构的项目. NLayerAppV3是在NLayerAppV2的基础上,使用.net core2.1进行重新构建的:它包含了 ...
- 【加密算法】AES
一.简介 AES(Advanced Encryption Standard):高级加密标准,是下一代的加密算法标准,速度快,安全级别高. 用AES加密2000年10月,NIST(美国国家标准和技术协会 ...