《Asp.Net Core3 + Vue3入坑教程》 此教程適合新手入門或者先後端分離嘗試者。能夠根據圖文一步一步進操做編碼也能夠選擇直接查看源碼。每一篇文章都有對應的源碼html
教程後期會將 .Net Core 3升級成 .Net Core 5前端
Asp.Net Core後端項目vue
Vue3 前端項目git
暫未發表敬請期待...github
本文爲《Asp.Net Core3 + Vue3入坑教程》系列教程的後端第四篇 - EF Core & Postgresql。上文已經爲Simple項目增長了Restful API 可是數據是模擬出來的,本文繼續爲Simple項目增長與Postgresql數據庫的鏈接,並使用EF Core ORM框架實現與數據庫的交互。sql
直接進官網下載 https://www.postgresql.org/數據庫
也能夠不安裝navicat,使用其餘數據庫客戶端json
官網下載 http://www.navicat.com.cn/後端
代碼以下:api
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Newtonsoft.Json.Serialization; using Simple_Asp.Net_Core.Data; using Simple_Asp.Net_Core.ServiceProvider; using System; namespace Simple_Asp.Net_Core { public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddDbContext<CommanderContext>(options => options.UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=123456")); services.AddCORS(); services.AddMvc(); services.AddSwagger(); services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); services.AddScoped<ICommanderRepo, MockCommanderRepo>(); services.AddControllers().AddNewtonsoftJson(s => { s.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); }); } // 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.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "ApiHelp V1"); }); } app.UseCors("CorsTest"); app.UseRouting(); app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute()); } } }
使用 EF Core 時,數據訪問是經過使用模型來執行的。 模型由(域模型)實體類和表示與數據庫的會話的派生上下文 (DbContext) 組成。更多內容參考 https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.entityframeworkcore.dbcontext?view=efcore-5.0
代碼以下
using Microsoft.EntityFrameworkCore; using Simple_Asp.Net_Core.Models; namespace Simple_Asp.Net_Core.Data { public class CommanderContext : DbContext { public CommanderContext(DbContextOptions<CommanderContext> opt) : base(opt) { } public DbSet<Command> Commands { get; set; } } }
using Simple_Asp.Net_Core.Models; using System; using System.Collections.Generic; using System.Linq; namespace Simple_Asp.Net_Core.Data { public class SqlCommanderRepo : ICommanderRepo { private readonly CommanderContext _context; public SqlCommanderRepo(CommanderContext context) { _context = context; } public void CreateCommand(Command cmd) { if (cmd == null) { throw new ArgumentNullException(nameof(cmd)); } _context.Commands.Add(cmd); } public void DeleteCommand(Command cmd) { if (cmd == null) { throw new ArgumentNullException(nameof(cmd)); } _context.Commands.Remove(cmd); } public IEnumerable<Command> GetAllCommands() { return _context.Commands.ToList(); } public Command GetCommandById(int id) { return _context.Commands.FirstOrDefault(p => p.Id == id); } public bool SaveChanges() { return (_context.SaveChanges() >= 0); } public void UpdateCommand(Command cmd) { //Nothing } } }
services.AddScoped<ICommanderRepo, MockCommanderRepo>(); => services.AddScoped<ICommanderRepo, SqlCommanderRepo>();
接下來會使用到 .Net Core CLI命令,具體能夠參考 https://docs.microsoft.com/zh-cn/dotnet/core/tools/
命令以下:
dotnet tool install --global dotnet-ef
命令以下:
dotnet ef migrations add InitialMigration
由於當前文件夾找不到解決方案,因此報錯了
命令以下:
cd Simple_Asp.Net_Core dotnet ef migrations add InitialMigration
cd Simple_Asp.Net_Core dotnet ef database update
本文爲Simple項目增長與Postgresql數據庫的鏈接,這裏數據庫能夠替換成其餘數據庫,只需引入正確Nuget包,以及調整數據庫鏈接配置便可!本文應用的是EF Core裏的Code First方式進行開發,EF Core的內容不少本文只有簡單的使用,更多知識能夠參考 https://docs.microsoft.com/en-us/ef/core/ 文檔進行深刻學習!
注意:源碼調試過程當中若是出現xml文件路徑錯誤,須要參照第一章(後端項目搭建與Swagger配置步驟)Swagger配置「配置XML 文檔文件」步驟,取消勾選而後再選中 ,將XML路徑設置成與你的電腦路徑匹配!
EF Core官網文檔(推薦學習) https://docs.microsoft.com/en-us/ef/core/
.Net Core CLI命令 https://docs.microsoft.com/zh-cn/dotnet/core/tools/
微軟官方文檔 https://docs.microsoft.com/zh-cn/aspnet/core/?view=aspnetcore-5.0