From ea78be46be34617cfa53d491e337778bf13d880f Mon Sep 17 00:00:00 2001 From: yb <1728066681@qq.com> Date: Wed, 23 Jun 2021 17:47:13 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1webapi=E6=8F=90?= =?UTF-8?q?=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...t \345\221\275\344\273\244\357\274\211.md" | 0 .../Web.Api/.vscode/launch.json" | 17 +++++++ .../Web.Api/Web.Api.sln" | 34 +++++++++++++ .../Web.Api/Controllers/UsersController.cs" | 51 +++++++++++++++++++ .../Controllers/WeatherForecastController.cs" | 39 ++++++++++++++ .../Web.Api/Web.Api/Entity/Users.cs" | 17 +++++++ .../Web.Api/Web.Api/Program.cs" | 26 ++++++++++ .../Web.Api/Properties/launchSettings.json" | 30 +++++++++++ .../Web.Api/Web.Api/Startup.cs" | 48 +++++++++++++++++ .../Web.Api/Web.Api/WeatherForecast.cs" | 15 ++++++ .../Web.Api/Web.Api/Web.Api.csproj" | 8 +++ .../Web.Api/appsettings.Development.json" | 9 ++++ .../Web.Api/Web.Api/appsettings.json" | 10 ++++ .../Web.Api/api.http" | 16 ++++++ 14 files changed, 320 insertions(+) rename "\346\242\201\350\211\272\347\274\244/Vue - 2021.06.19\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" => "\346\242\201\350\211\272\347\274\244/Vue - 2021.06.22\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" (100%) create mode 100644 "\346\242\201\350\211\272\347\274\244/Web.Api/.vscode/launch.json" create mode 100644 "\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api.sln" create mode 100644 "\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Controllers/UsersController.cs" create mode 100644 "\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Controllers/WeatherForecastController.cs" create mode 100644 "\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Entity/Users.cs" create mode 100644 "\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Program.cs" create mode 100644 "\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Properties/launchSettings.json" create mode 100644 "\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Startup.cs" create mode 100644 "\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/WeatherForecast.cs" create mode 100644 "\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Web.Api.csproj" create mode 100644 "\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/appsettings.Development.json" create mode 100644 "\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/appsettings.json" create mode 100644 "\346\242\201\350\211\272\347\274\244/Web.Api/api.http" diff --git "a/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.19\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.22\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" similarity index 100% rename from "\346\242\201\350\211\272\347\274\244/Vue - 2021.06.19\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" rename to "\346\242\201\350\211\272\347\274\244/Vue - 2021.06.22\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/.vscode/launch.json" "b/\346\242\201\350\211\272\347\274\244/Web.Api/.vscode/launch.json" new file mode 100644 index 0000000..2ef4b09 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/.vscode/launch.json" @@ -0,0 +1,17 @@ +{ + // 使用 IntelliSense 了解相关属性。 + // 悬停以查看现有属性的描述。 + // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "pwa-node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${file}" + } + ] +} \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api.sln" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api.sln" new file mode 100644 index 0000000..2525f92 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api.sln" @@ -0,0 +1,34 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Web.Api", "Web.Api\Web.Api.csproj", "{2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Debug|x64.ActiveCfg = Debug|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Debug|x64.Build.0 = Debug|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Debug|x86.ActiveCfg = Debug|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Debug|x86.Build.0 = Debug|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Release|Any CPU.Build.0 = Release|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Release|x64.ActiveCfg = Release|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Release|x64.Build.0 = Release|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Release|x86.ActiveCfg = Release|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Controllers/UsersController.cs" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Controllers/UsersController.cs" new file mode 100644 index 0000000..c938a0e --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Controllers/UsersController.cs" @@ -0,0 +1,51 @@ +using Microsoft.AspNetCore.Mvc; +using System.Collections.Generic; +using Web.Api.Entity; +using System.Linq; + +namespace Web.Api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class UsersController : ControllerBase{ + [HttpGet] + public dynamic Get(){ + var users = GetUsers(); + return users; + } + [HttpGet("{id}")] + public dynamic Get(int id){ + var users = GetUsers(); + var user = users.Where(x=>x.id==id).FirstOrDefault(); + return user; + } + [HttpPost("{id}")] + public dynamic Post(dynamic model){ + return new { + Code = 2000, + Date = model, + Msg = "土笨笨", + }; + } + public IEnumerable GetUsers(){ + var users = new List{ + new Users{ + Id=1, + Username="good", + Password="lisa" + }, + new Users{ + Id=2, + Username="good", + Password="nice" + }, + new Users{ + Id=3, + Username="good", + Password="wdnbd" + }, + }; + return users; + } + } +} \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Controllers/WeatherForecastController.cs" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Controllers/WeatherForecastController.cs" new file mode 100644 index 0000000..3b56fb4 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Controllers/WeatherForecastController.cs" @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; + +namespace Web.Api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpGet] + public IEnumerable Get() + { + var rng = new Random(); + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateTime.Now.AddDays(index), + TemperatureC = rng.Next(-20, 55), + Summary = Summaries[rng.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Entity/Users.cs" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Entity/Users.cs" new file mode 100644 index 0000000..9a7404e --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Entity/Users.cs" @@ -0,0 +1,17 @@ +using System; +namespace Web.Api.Entity +{ + public class Users{ + public Users(){ + createdTime = DateTime.Now; + updatedTime = DateTime.Now; + Remarks = ""; + } + public int Id {get;set;} + public string Username {get;set;} + public string Password {get;set;} + public DateTime createdTime{get;set;} + public DateTime updatedTime{get;set;} + public string Remarks {get;set;} + } +} \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Program.cs" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Program.cs" new file mode 100644 index 0000000..11cd3c8 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Program.cs" @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace Web.Api +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Properties/launchSettings.json" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Properties/launchSettings.json" new file mode 100644 index 0000000..e36e127 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Properties/launchSettings.json" @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:42799", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Web.Api": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Startup.cs" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Startup.cs" new file mode 100644 index 0000000..df11904 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Startup.cs" @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace Web.Api +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/WeatherForecast.cs" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/WeatherForecast.cs" new file mode 100644 index 0000000..699931c --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/WeatherForecast.cs" @@ -0,0 +1,15 @@ +using System; + +namespace Web.Api +{ + public class WeatherForecast + { + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string Summary { get; set; } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Web.Api.csproj" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Web.Api.csproj" new file mode 100644 index 0000000..d12c450 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Web.Api.csproj" @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + + + + diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/appsettings.Development.json" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/appsettings.Development.json" new file mode 100644 index 0000000..8983e0f --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/appsettings.Development.json" @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/appsettings.json" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/appsettings.json" new file mode 100644 index 0000000..d9d9a9b --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/appsettings.json" @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/api.http" "b/\346\242\201\350\211\272\347\274\244/Web.Api/api.http" new file mode 100644 index 0000000..9988738 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/api.http" @@ -0,0 +1,16 @@ +###获取用户列表 +GET http://localhost:5000/users HTTP/1.1 + + +###获取指定ID用户 +GET http://localhost:5000/users3 HTTP/1.1 + + +###创建用户 +POST http://localhost:5000/users HTTP/1.1 +Content-Type: application/json + +{ + "username":"good", + "password":"土笨笨" +} \ No newline at end of file -- Gitee From bd3ddebe0f4bd0b68e1768a24fb179e16517357e Mon Sep 17 00:00:00 2001 From: yb <1728066681@qq.com> Date: Sat, 26 Jun 2021 17:46:57 +0800 Subject: [PATCH 2/3] Ef Core --- ...t \345\221\275\344\273\244\357\274\211.md" | 0 ...1.06.26\357\274\210EF Core\357\274\211.md" | 63 +++++++++++++++++++ .../Web.Api/.vscode/launch.json" | 17 ----- .../Web.Api/Controllers/UsersController.cs" | 17 ++++- .../Web.Api/Web.Api/Date/Admin3000Db.cs" | 0 .../Web.Api/Web.Api/Startup.cs" | 2 +- .../Web.Api/Web.Api/Web.Api.csproj" | 5 ++ .../Web.Api/api.http" | 4 +- 8 files changed, 87 insertions(+), 21 deletions(-) create mode 100644 "\346\242\201\350\211\272\347\274\244/Vue - 2021.06.23\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" create mode 100644 "\346\242\201\350\211\272\347\274\244/Vue - 2021.06.26\357\274\210EF Core\357\274\211.md" delete mode 100644 "\346\242\201\350\211\272\347\274\244/Web.Api/.vscode/launch.json" create mode 100644 "\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Date/Admin3000Db.cs" diff --git "a/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.23\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.23\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" new file mode 100644 index 0000000..e69de29 diff --git "a/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.26\357\274\210EF Core\357\274\211.md" "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.26\357\274\210EF Core\357\274\211.md" new file mode 100644 index 0000000..3a16490 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.26\357\274\210EF Core\357\274\211.md" @@ -0,0 +1,63 @@ +# 安装 EF Core + +### 获取 Entity Framework Core 运行时 + +把EF Core添加到项目中,要使用的数据库提供程序的 NuGet 包 + +命名 +~~~html +dotnet add package Microsoft.EntityFrameworkCore.Sql数据库类型 +~~~ + +创建模型 + +``` +using System.Collections.Generic; +using Microsoft.EntityFrameworkCore; + +namespace EFGetStarted +{ + public class BloggingContext : DbContext + { + public DbSet Blogs { get; set; } + public DbSet Posts { get; set; } + + // The following configures EF to create a Sqlite database file as `C:\blogging.db`. + // For Mac or Linux, change this to `/tmp/blogging.db` or any other absolute path. + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlite(@"Data Source=C:\blogging.db"); + } + + public class Blog + { + public int BlogId { get; set; } + public string Url { get; set; } + + public List Posts { get; } = new List(); + } + + public class Post + { + public int PostId { get; set; } + public string Title { get; set; } + public string Content { get; set; } + + public int BlogId { get; set; } + public Blog Blog { get; set; } + } +} +``` +创建数据库 + ++ 以下步骤使用迁移创建数据库 + +命令 +``` +dotnet tool install --global dotnet-ef + +dotnet add package Microsoft.EntityFrameworkCore.Design + +dotnet ef migrations add InitialCreate + +dotnet ef database update +``` \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/.vscode/launch.json" "b/\346\242\201\350\211\272\347\274\244/Web.Api/.vscode/launch.json" deleted file mode 100644 index 2ef4b09..0000000 --- "a/\346\242\201\350\211\272\347\274\244/Web.Api/.vscode/launch.json" +++ /dev/null @@ -1,17 +0,0 @@ -{ - // 使用 IntelliSense 了解相关属性。 - // 悬停以查看现有属性的描述。 - // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "type": "pwa-node", - "request": "launch", - "name": "Launch Program", - "skipFiles": [ - "/**" - ], - "program": "${file}" - } - ] -} \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Controllers/UsersController.cs" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Controllers/UsersController.cs" index c938a0e..10625f7 100644 --- "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Controllers/UsersController.cs" +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Controllers/UsersController.cs" @@ -24,9 +24,24 @@ namespace Web.Api.Controllers return new { Code = 2000, Date = model, - Msg = "土笨笨", + Msg = "成功", }; } + [HttpPut("{id}")] + public dynamic Put(int id, dynamic model){ + return new{ + Date = model, + Msg=string.Format("要修改的ID为{0},",id) + }; + } + [HttpDelete("{id}")] + public dynamic Delete(int id,dynamic model){ + return new{ + Date = model, + Msg=string.Format("要修改的ID为{0},",id) + }; + } + public IEnumerable GetUsers(){ var users = new List{ new Users{ diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Date/Admin3000Db.cs" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Date/Admin3000Db.cs" new file mode 100644 index 0000000..e69de29 diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Startup.cs" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Startup.cs" index df11904..009492d 100644 --- "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Startup.cs" +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Startup.cs" @@ -23,7 +23,7 @@ namespace Web.Api // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) - { + { services.AddControllers(); } diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Web.Api.csproj" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Web.Api.csproj" index d12c450..2efd60a 100644 --- "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Web.Api.csproj" +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Web.Api.csproj" @@ -4,5 +4,10 @@ netcoreapp3.1 + + + + + diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/api.http" "b/\346\242\201\350\211\272\347\274\244/Web.Api/api.http" index 9988738..a84c8d1 100644 --- "a/\346\242\201\350\211\272\347\274\244/Web.Api/api.http" +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/api.http" @@ -5,7 +5,6 @@ GET http://localhost:5000/users HTTP/1.1 ###获取指定ID用户 GET http://localhost:5000/users3 HTTP/1.1 - ###创建用户 POST http://localhost:5000/users HTTP/1.1 Content-Type: application/json @@ -13,4 +12,5 @@ Content-Type: application/json { "username":"good", "password":"土笨笨" -} \ No newline at end of file +} + -- Gitee From 3ea9e0620ba32c22d843e9293aaa62a873b987ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E8=89=BA=E7=BC=A4?= <1728066681@qq.com> Date: Mon, 28 Jun 2021 14:29:53 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E7=AC=94=E8=AE=B0=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...t \345\221\275\344\273\244\357\274\211.md" | 17 ++++++++++++ ...1.06.26\357\274\210EF Core\357\274\211.md" | 27 ++++++++++++++++--- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git "a/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.23\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.23\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" index e69de29..8707d9c 100644 --- "a/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.23\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" +++ "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.23\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" @@ -0,0 +1,17 @@ +# 安装REST CLient 插件 + +## 控制器中用法 + +``` +// GET +[HttpGet("{id}")] + +// POST +[HttpPost] + +// PUT +[HttpPut("{id}")] + +// Delete +[HttpDelete("{id}")] +``` \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.26\357\274\210EF Core\357\274\211.md" "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.26\357\274\210EF Core\357\274\211.md" index 3a16490..7a0a351 100644 --- "a/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.26\357\274\210EF Core\357\274\211.md" +++ "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.26\357\274\210EF Core\357\274\211.md" @@ -1,3 +1,5 @@ +# WebApi 连接数据库 + # 安装 EF Core ### 获取 Entity Framework Core 运行时 @@ -5,11 +7,28 @@ 把EF Core添加到项目中,要使用的数据库提供程序的 NuGet 包 命名 -~~~html -dotnet add package Microsoft.EntityFrameworkCore.Sql数据库类型 -~~~ +``` +// sqlserver/sqlite + +dotnet add package Microsoft.EntityFrameworkCore.Sql数据库类型(sqlserver/sqlite) +``` + +## 数据库迁移 +``` +// 安装dotnet-ef +dotnet tool install --global dotnet-ef + +// 安装EFCore.Design +dotnet add package Microsoft.EntityFrameworkCore.Design + +// 创建迁移,指示 EF Core 创建名为 InitialCreate 的迁移 +dotnet ef migrations add InitialCreate + +// 创建数据库和架构 +dotnet ef database update +``` -创建模型 +创建模型 Data 部分配置演示 ``` using System.Collections.Generic; -- Gitee