《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