日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

dotnet vs code mysql_.net 5 用vs code链接mysql体验

發(fā)布時間:2024/4/17 数据库 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 dotnet vs code mysql_.net 5 用vs code链接mysql体验 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

初學(xué).net5,不想下載vs,就想用手頭的vs code擼一下restful api,并且數(shù)據(jù)庫選用mysql(因為便宜,方便),但是在鏈接數(shù)據(jù)庫的時候遇到了不少坑,此文只簡單記錄一下。

建立.net 5程序,首先要下載.net 5 sdk。在vs code編寫.net 5的程序,則要安裝c#等擴展,以下是一個大佬寫的,比較詳細,按照這個步驟即可。

在安裝好初步的環(huán)境后,就是創(chuàng)建restful api。是在目標文件夾下,在終端內(nèi)輸入:

dotnetnew webapi 或者dotnetnew webapi -o 指定文件夾 命令

然后你會發(fā)現(xiàn)相關(guān)項目文件就被這么創(chuàng)建好了。圖片就不上了,自己看。

然后,要給vs code安裝nuget擴展包,nuget是管理.net core程序擴展包的程序,類似于php的composer或者js的npm。

在vs code插件市場里搜索安裝一下很簡單的就安裝上了。

安裝好后,按ctrl+shift+p,輸入NuGet Package Manager:Add Package

然后選擇版本號。

此文選用的是pomelo寫的程序集來鏈接mysql。當然還可以選擇其他的。

筆者比較蠢,在這個問題上糾結(jié)了很久,其實主要是將依賴版本當做了.net版本。沒能屢清楚.net 5 .net core 和entity framework core等的關(guān)系。而其實這里的版本號是指的entity framework的版本號。

Entity Framework Core 是適用于 .NET 的新式對象數(shù)據(jù)庫映射器。 它支持 LINQ 查詢、更改跟蹤、更新和架構(gòu)遷移。 EF Core 適用于很多數(shù)據(jù)庫,包括 SQL 數(shù)據(jù)庫(本地和 Azure)、SQLite、MySQL、PostgreSQL 和 Azure Cosmos DB。

以上摘自微軟官方。

所以,要選擇鏈接mysql的程序集,就要選擇安裝相關(guān)依賴版本的entity framework,否則就會報錯。

在安裝好環(huán)境后,修改appsettings.json如下:

{

"ConnectionStrings": {

"DefaultConnection": "server=ip address;userid=test;pwd=password;port=3306;database=dotnet_test;sslmode=none;CharSet=utf8;"},

"Logging": {

"LogLevel": {

"Default": "Information",

"Microsoft": "Warning",

"Microsoft.Hosting.Lifetime": "Information"

}

},

"AllowedHosts": "*"

}

然后修改startup.cs,我的理解是入口文件。

1 using System;

2 using System.Collections.Generic;

3 using System.Linq;

4 using System.Threading.Tasks;

5 using Microsoft.AspNetCore.Builder;

6 using Microsoft.AspNetCore.Hosting;

7 using Microsoft.AspNetCore.HttpsPolicy;

8 using Microsoft.AspNetCore.Mvc;

9 using Microsoft.Extensions.Configuration;

10 using Microsoft.Extensions.DependencyInjection;

11 using Microsoft.Extensions.Hosting;

12 using Microsoft.Extensions.Logging;

13 using Microsoft.OpenApi.Models;

14 using Microsoft.EntityFrameworkCore;

15 using Pomelo.EntityFrameworkCore.MySql.Infrastructure;16 using webapi.Models;

17

18 namespace webapi

19 {

20 public class Startup

21 {

22 public Startup(IConfiguration configuration)

23 {

24 Configuration = configuration;

25 }

26

27 public IConfiguration Configuration { get; }

28

29 // This method gets called by the runtime. Use this method to add services to the container.

30 public void ConfigureServices(IServiceCollection services)

31 {

32

33string connectionString = Configuration.GetSection("ConnectionStrings").GetSection("DefaultConnection").Value;

34 // Replace "YourDbContext" with the name of your own DbContext derived class.

35 services.AddDbContextPool(

36 dbContextOptions => dbContextOptions

37 .UseMySql(

38 // Replace with your connection string.

39 connectionString,

40 // Replace with your server version and type.

41 mySqlOptions => mySqlOptions

42 .ServerVersion(new Version(5, 7, 31), ServerType.MySql)

43 .CharSetBehavior(CharSetBehavior.NeverAppend)

44 )

45 );46

47 services.AddControllers();

48 services.AddSwaggerGen(c =>

49 {

50 c.SwaggerDoc("v1", new OpenApiInfo { Title = "webapi", Version = "v1" });

51 });

52 }

53

54 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

55 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

56 {

57 if (env.IsDevelopment())

58 {

59 app.UseDeveloperExceptionPage();

60 app.UseSwagger();

61 app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "webapi v1"));

62 }

63

64 app.UseHttpsRedirection();

65

66 app.UseRouting();

67

68 app.UseAuthorization();

69

70 app.UseEndpoints(endpoints =>

71 {

72 endpoints.MapControllers();

73 });

74 }

75 }

76 }

再建立個models文件夾,新建一個數(shù)據(jù)庫上下文文件,當然這個文件也可以放在根目錄,請根據(jù)自己的習慣設(shè)置。我是在models文件夾下建立了appDb.cs文件。

using Microsoft.EntityFrameworkCore;

namespace webapi.Models

{

public class AppDb : DbContext

{

public DbSet test { get; set; } //創(chuàng)建實體類添加Context中,我的表只有test這一個哦

public AppDb(DbContextOptions options) : base(options)

{

}

}

}

再在models下建立數(shù)據(jù)表的model文件TestModels.cs。(數(shù)據(jù)庫和表請自己創(chuàng)建,這里略去了。)

using System.ComponentModel.DataAnnotations;

namespace webapi.Models

{

public class test

{

[Key]

public int id { get; set; }

[MaxLength(20)]

public string name { get; set; }

[MaxLength(300)]

public string content { get; set; }

}

}

最后是在controllers文件夾下建立控制器文件TestControllers.cs

using Microsoft.AspNetCore.Mvc;

using System.Collections.Generic;

using System.Linq;

using webapi.Models;

namespace webapi.Controllers

{

[Route("api/[controller]/[action]")]

[ApiController]

public class TestController : ControllerBase

{

private readonly AppDb _db;

public TestController(AppDb db)

{

_db = db;

}

// GET api/test

[HttpGet]

public List Get()

{

return _db.Set().ToList();

}

}

}

最后,按ctrl+f5進行調(diào)試。

在地址后輸入/api/controller/action,action和controller自己定。可以看到,數(shù)據(jù)讀取成功。

原文:https://www.cnblogs.com/delgoh/p/14250311.html

總結(jié)

以上是生活随笔為你收集整理的dotnet vs code mysql_.net 5 用vs code链接mysql体验的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。