ASP.NET Core微服务(一)——【完整API搭建及访问过程】
ASP.NET Core微服務(wù)(一)——【完整API搭建及訪問(wèn)過(guò)程】:
環(huán)境:win10專業(yè)版+vs2019+sqlserver2014/2019
對(duì)應(yīng)練習(xí)demo下載路徑(1積分):【https://download.csdn.net/download/feng8403000/15134527】
對(duì)應(yīng)練習(xí)sql下載路徑(0積分):【https://download.csdn.net/download/feng8403000/15134699】
1、創(chuàng)建API項(xiàng)目
2、項(xiàng)目層級(jí)以及作用
3、引入數(shù)據(jù)庫(kù)·這里采用的是sqlserver2014版本,如果是2019以上版本,數(shù)據(jù)庫(kù)連接的地址請(qǐng)勿使用【127.0.0.1】,或者用【.】通用即可
3.1、添加NuGe程序包
3.2、添加4個(gè)需要的包
安裝步驟:
四個(gè)包如上安裝即可,查看是否安裝成功
或
3.3、通過(guò)【工具】->【NuGet包管理器】->【程序包管理器控制臺(tái)】
自動(dòng)連接寫法·sqlserver2019【Data Source=.】
Scaffold-Dbcontext 'Data Source=127.0.0.1;Initial Catalog=數(shù)據(jù)庫(kù)name;Integrated Security=True;' Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context 自定義nameContext
密碼鏈接寫法·sqlserver2019【Data Source=.】
Scaffold-Dbcontext 'Data Source=127.0.0.1;Initial Catalog=數(shù)據(jù)庫(kù)name;User Id=用戶名;Password=密碼;' Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context 自定義nameContext
這樣,我們就有了可以操作的EF對(duì)象了。
4、添加【Startup.cs】服務(wù)配置
命名空間需求:
using Microsoft.EntityFrameworkCore;using WebApiDemo.Models;所需代碼:
services.AddDbContext<Girl1804Context>(options =>{options.UseSqlServer(Configuration.GetConnectionString("Girl1804DB"));});添加位置:
【appsetting.json】配置文件配置:
配置字符串:?
"ConnectionStrings": {"Girl1804DB": "Data Source=127.0.0.1;Initial Catalog=girl1804;Integrated Security=True;"},配置位置:
賬號(hào)密碼的方式:【Data Source=服務(wù)器地址;Initial Catalog=數(shù)據(jù)庫(kù)name;User Id=用戶名;Password=密碼;】
5、EF,通過(guò)構(gòu)造方法注入測(cè)試
5.1、添加【api控制器】
5.2添加構(gòu)造方法注入EF生成的類【Girl1804Context】
5.3、配置路由【[Route("api/[controller]/[action]")]】以及創(chuàng)建查詢測(cè)試EF
public object GetInfo() {return db.GirlSixes.ToList();}5.4、啟動(dòng)項(xiàng)目,并訪問(wèn)【api接口】,路徑為:【http://localhost:5000/api/Test/GetInfo】
【Ctrl+F5】啟動(dòng)
查看瀏覽器:默認(rèn)打開路徑應(yīng)為【http://localhost:5000/weatherforecast】
輸入:【http://localhost:5000/api/Test/GetInfo】進(jìn)行數(shù)據(jù)訪問(wèn)測(cè)試。
6、增加函數(shù)【Add】·在【TestController】?jī)?nèi)完成
/// <summary>/// 添加方法/// </summary>/// <param name="nickName"></param>/// <param name="introduce"></param>/// <returns></returns>public bool Add(string nickName,string introduce) {GirlSix g = new GirlSix();g.Id = System.Guid.NewGuid().ToString("N");g.CreateDate = DateTime.Now;g.NickName = nickName;g.Introduce = introduce;db.GirlSixes.Add(g);int rows = db.SaveChanges();return rows > 0 ?true:false;}?
7、修改【SelectById】&【Update】函數(shù)·在【TestController】?jī)?nèi)完成
/// <summary>/// 單個(gè)查詢/// </summary>/// <param name="id"></param>/// <returns></returns>public object SelectById(string id) {return db.GirlSixes.Where(o => o.Id == id).SingleOrDefault();}/// <summary>/// 修改方法/// </summary>/// <param name="id"></param>/// <param name="nickName"></param>/// <param name="introduce"></param>/// <returns></returns>public bool Update(string id,string nickName, string introduce) {GirlSix girlSix = db.GirlSixes.Where(o => o.Id == id).SingleOrDefault();if (girlSix == null) {return false;}girlSix.NickName = nickName;girlSix.Introduce = introduce;int rows = db.SaveChanges();return rows > 0 ? true : false;}?
8、刪除函數(shù)【Del】·在【TestController】?jī)?nèi)完成
/// <summary>/// 刪除方法/// </summary>/// <param name="id"></param>/// <returns></returns>public bool Del(string id) {GirlSix girlSix = db.GirlSixes.Where(o => o.Id == id).SingleOrDefault();if (girlSix == null){return false;}db.GirlSixes.Remove(girlSix);int rows = db.SaveChanges();return rows > 0 ? true : false;}?
9、postman測(cè)試·【Ctrl+F5】啟動(dòng)后,根據(jù)對(duì)應(yīng)的接口路徑進(jìn)行測(cè)試。
9.1查詢所有測(cè)試:
9.2、增加測(cè)試
9.3、修改測(cè)試
?
9.4、刪除測(cè)試
10、總結(jié)
a)、EF包4個(gè),別錯(cuò)嘍
b)、根據(jù)對(duì)應(yīng)的數(shù)據(jù)庫(kù)使用對(duì)應(yīng)的引入方式,本地用直接連,遠(yuǎn)程用賬號(hào)密碼連。
c)、在ASP.NET Core中引入的EF如果要使用多表聯(lián)合查詢建議使用Linq進(jìn)行操作。
d)、在修改操作時(shí)直接SaveChanges操作即可。
希望此文對(duì)大家有所幫助,后續(xù)會(huì)編寫
ASP.NET Core微服務(wù)(二)——【ASP.NET Core Swagger配置】、
ASP.NET Core微服務(wù)(三)——【跨域配置】、
ASP.NET Core微服務(wù)(四)——【靜態(tài)vue使用axios解析接口】、
ASP.NET Core微服務(wù)(五)——【vue腳手架解析接口】、
ASP.NET Core微服務(wù)(六)——【redis操作】、
ASP.NETCore微服務(wù)(七)——【docker部署linux上線】
等文章。
此文標(biāo)題為ASP.NET Core微服務(wù)(一)——【完整API搭建及訪問(wèn)過(guò)程】
?
請(qǐng)關(guān)注,后續(xù)內(nèi)容很快更新。
總結(jié)
以上是生活随笔為你收集整理的ASP.NET Core微服务(一)——【完整API搭建及访问过程】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CPU与GPU的区别
- 下一篇: ASP.NET Core微服务(二)——