按照通常的套路,首先创建一个空白的解决方案,需要用到.netcore sdk命令:

dotnet new sln -o dotnetcore_tutrorial

这个时候可以看到在目标目录下生成了一个同名的.sln文件,这个和使用vs是一样的,在我们实际开发过程中,通常要建立运行项目(web项目或者console项目),多个类库项目,以及单元测试项目。

首先建立一个类库项目,并将该项目加入到解决方案中:

dotnet new classlib -o DotNetTurorial.Common
dotnet sln add DotNetTurorial.Common/DotNetTurorial.Common.cspro

ps:最好把类库项目创建在dotnetcore_tutrorial目录下,这样可以保证.sln文件和项目文件在同一个目录下

通过同样的方式创建控制台项目和单元测试项目

dotnet new console -o DotNetTurorial.ConsoleApp
dotnet sln add DotNetTurorial.ConsoleApp/DotNetTurorial.ConsoleApp.csproj
dotnet new xunit -o DotNetTurorial.UnitTest
dotnet sln add DotNetTurorial.UnitTest/DotNetTurorial.UnitTest.csproj

现在整个项目的结构已经建立完成,我们用vscode打开解决方案对应的文件夹,目录结构如下:

接下来需要添加项目引用,也就是console项目需要引用类库项目:

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DotNetTurorial.Common\DotNetTurorial.Common.csproj"/>
</ItemGroup>
</Project>

同样的方法添加到测试项目:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
<TargetFramework>netcoreapp2.</TargetFramework> <IsPackable>false</IsPackable>
</PropertyGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170427-09" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DotNetTurorial.Common\DotNetTurorial.Common.csproj"/>
</ItemGroup>
</Project>

配置好以后,切换到Console项目所在目录,执行dotnet restore 初始化项目,执行dotnet build 编译项目

接下来实现一个简单的业务逻辑,通过console程序添加学生信息,并把数据存入mysql中:

操作数据需要用到几个nuget包,需要在项目文件中手动配置(dotnetcore2.0 不需要再引入NETStandard.Library)。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="1.50.2"/>
<PackageReference Include="NETStandard.Library" Version="1.6.0"/>
<PackageReference Include="SapientGuardian.MySql.Data" Version="6.9.813"/>
</ItemGroup>
</Project>

这里建议安装一个vscode插件nuget package manager,可以通过该插件快速安装nuget包,安装完成以后在项目文件中右键,选择命令面板

在命令面板中输入nuget,可以根据包名搜索出该包的不同版本,添加以后使用dotnet resotore 命令还原nuget库到依赖

在common项目中增加一个数据库操作类:

using System;
using Dapper;
using MySql.Data.MySqlClient; namespace DotNetTurorial.Common
{
public class SQLHelper
{
public const string _conStr="server=127.0.0.1;port=3306;user id=root;password=123456;database=dotnetcore;pooling=false";
public static int AddStudent(string name,int gender,string phone)
{
MySqlConnection connect=new MySqlConnection(_conStr);
var ret = connect.Execute("insert into student(name,gender,phone) values(@name,@gender,@phone)",new {name=name,gender=gender,phone=phone});
return ret;
}
}
}

在console项目中增加输入相关控制代码:

using System;
using DotNetTurorial.Common; namespace DotNetTurorial.ConsoleApp
{
class Program
{
static void Main(string[] args)
{
//Console.OutputEncoding = Encoding.UTF8; // 设置控制台编码
AddUser();
while(Console.ReadLine()!="exit"){
AddUser();
}
}
static void AddUser()
{
Console.WriteLine("please enter name:");
var name=Console.ReadLine();
Console.WriteLine("please enter gender,1 for male and 0 for female.");
var gender = Console.ReadLine();
Console.WriteLine("please enter phone:");
var phone = Console.ReadLine(); var ret = SQLHelper.AddStudent(name,Convert.ToInt32(gender),phone);
if(ret>)
{
Console.WriteLine("success");
}
}
}
}

在终端中输入dotnet run 运行程序

如果需要调试,需要安装C#扩展:

如果调试有输入到console程序,需要修改下:launch.json,注释掉"Console":"internalConsole", 增加:"externalConsole":true, 否则无法数据

{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceRoot}/DotNetTurorial.ConsoleApp/bin/Debug/netcoreapp2.0/DotNetTurorial.ConsoleApp.dll",
"args": [],
"cwd": "${workspaceRoot}/DotNetTurorial.ConsoleApp",
// For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
//"console": "internalConsole",
"externalConsole":true, //使用外置的控制台
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}

源码地址:https://github.com/xienb/DotNetCore_Turorial.git

Ubuntu16.10下使用VSCode开发.netcore的更多相关文章

  1. Ubuntu16.04下配置VScode的C/C++开发环境

    博客转载:https://blog.csdn.net/weixin_43374723/article/details/84064644 Visual studio code是微软发布的一个运行于 Ma ...

  2. ubuntu16.10下安装erlang和RabbitMQ

    Ubuntu系统下安装RabbitMQ(我选择的是Ubuntu Server 16.10) 1.首先必须要有Erlang环境支持 --安装之前要装一些必要的库(Erlang开发环境同样)(参考:duq ...

  3. ubuntu16.04 下 C# mono开发环境搭建

    本文转自:https://www.cnblogs.com/2186009311CFF/p/9204031.html 前记 之前我一直不看好C#的前景,因为我认为它只能在windows下运行,不兼容,对 ...

  4. Ubuntu 14.10 下使用IDEA开发Spark应用

    1 环境准备 1.1 下载IDEA,可在官网下载 1.2 IDEA与Eclipse有点不同,IDEA中的New Projects相当于Eclipse中的workspace,New Module才是新建 ...

  5. windows 10下的python开发环境

    linux子系统 按照文档 https://www.jianshu.com/p/2bcf5eca5fbc 的前五步,完成 ubuntu子系统安装. 不需安装图形桌面,无使用价值. 在https://w ...

  6. Ubuntu16.04下部署golang开发环境

    一.需要文件 golang http://www.golangtc.com/download liteide http://www.golangtc.com/download/liteide 二.安装 ...

  7. ubuntu16.04 下使用vscode备忘录

    微软的vscode是为程序员做了非常大贡献,其强大的功能和各个平台的可移植性给vscode带来了非常大的火力.在程序员的世界中非常的流行,算是一线明星了. 我把使用过程中遇到的一些问题做个记录,方便自 ...

  8. 解决双系统(Window10+Ubuntu16.10)下ubuntu安装git时提示软件包git没有可安装候选问题

    选择升级系统: sudo apt-get update 升级之后再输入: sudo apt-get install git 可成功安装.

  9. Ubuntu16.10下mysql5.7的安装及远程访问配置

    如何安装mysql 1.sudo apt-get update,如果很慢或者失败,需要在软件和更新中选择最佳服务器,勾选所有互联网下载选项及去掉其他软件所有勾选项 2.sudo apt-get upg ...

随机推荐

  1. Linux下解包/打包,压缩/解压命令

    .tar 解包:tar xvf FileName.tar 打包:tar cvf fileName.tar DirName tar.gz和.tgz 解压:tar zxvf FileName.tar.zi ...

  2. vuex里mapState,mapGetters使用详解

    这次给大家带来vuex里mapState,mapGetters使用详解,vuex里mapState,mapGetters使用的注意事项有哪些,下面就是实战案例,一起来看一下. 一.介绍 vuex里面的 ...

  3. public,protected,private

    1.public:public表明该数据成员.成员函数是对所有用户开放的,所有用户都可以直接进行调用 2.private:private表示私有,私有的意思就是除了class自己之外,任何人都不可以直 ...

  4. Qt Model/View学习(二)

    Model和View的搭配使用 DEMO pro文件 #------------------------------------------------- # # Project created by ...

  5. CC4 表达方式----输赢

    “我要赢,不管付出什么,我一定要赢!”当我赢得时候,“我赢了!(欢呼)”.当我输的时候“不,我不要输.不开心.(垂头丧气)”.这样的场景你是否熟悉呢?我的一生都在经历输赢.以前我会为了赢一场游戏,花费 ...

  6. CentOS7.6 如何设置静态ip

    [root@localhost network-scripts]# cd /etc/sysconfig/network-scripts/[root@localhost network-scripts] ...

  7. Unity---资源管理中不同资源的路径获取方式

    1.首先需要先了解两个知识点: Unity内置的文件路径获取方式.windows的Directory.GetFiles文件获取方式:   1>Unity内置的文件路径获取方式,一下是官方解释:h ...

  8. js插件---iCheck是用来做什么的

    js插件---iCheck是用来做什么的 一.总结 一句话总结:25 种参数 用来定制复选框(checkbox)和单选按钮(radio button) 定制复选框 定制单选按钮 1.iCheck常用的 ...

  9. 数据结构(C语言版)-C语言和C++相关补充

    引用类型作形参的三点说明 (1)传递引用给函数与传递指针的效果是一样的,形参变化实参也发生变化.(2)引用类型作形参,在内存中并没有产生实参的副本,它直接对实参操作:而一般变量作参数,形参与实参就占用 ...

  10. spring cloud: zuul(二): zuul的serviceId/service-id配置(微网关)

    spring cloud: zuul(二): zuul的serviceId/service-id配置(微网关) zuul: routes: #路由配置表示 myroute1: #路由名一 path: ...