zoukankan      html  css  js  c++  java
  • netcore3.1API+efcore快速搭建

    netcore3.1API+efcore快速搭建

    框架结构

    实体层 Aer.Enties

    Models-->实体类

    业务层 After.IService

    IAltestitemController.cs

    业务层 After.Service

    AlltestitemService.cs

    接口层 AfterCore

    Controllers-> AlltestitemController.cs

    一 EF从数据库生成实体类到Enties

    1.执行以下语句安装依赖包

    Install-Package MySql.Data.EntityFrameworkCore -Pre
    Install-Package Pomelo.EntityFrameworkCore.MySql
    Install-Package Microsoft.EntityFrameworkCore.Tools
    Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design
    

    2.在程序包包管理器控制台

    Scaffold-DbContext "server=localhost;userid=root;pwd=1;port=3306;database=syerp;sslmode=none;" Pomelo.EntityFrameworkCore.MySql -OutputDir Models -Force
    

    二 业务层

    添加IAlltestitemService.cs接口

    using AfterCore.Models;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    
    namespace After.IService
    {
       public interface IAlltestitemService
        {
            /// <summary>
            /// 查询所有
            /// </summary>
            /// <returns></returns>
            Task<List<Alltestitem>> GetAllAsync();
    
            /// <summary>
            /// 查询总数
            /// </summary>
            /// <returns></returns>
            Task<int> CountAsync();
        }
    }
    
    

    After.Service接口实现

    AlltestitemService.cs

    using After.IService;
    using AfterCore.Models;
    using Microsoft.EntityFrameworkCore;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    
    namespace After.Service
    {
        public class AlltestitemService : IAlltestitemService
        {
            private readonly testContext _testDbContext;//DB
            public AlltestitemService(testContext testContexts)
            {
                _testDbContext = testContexts;
            }
    
            public async Task<int> CountAsync()
            {
                return await _testDbContext.Alltestitem.CountAsync();
            }
    
            public async Task<List<Alltestitem>> GetAllAsync()
            {
                return await _testDbContext.Alltestitem.ToListAsync();
    
            }
        }
    }
    
    

    三 注册DbContext

    appsettings.json

    "ConnectionStrings": {
        "DefaultConnection": "Server=;database=test;uid=root;pwd=m;"
      },
    

    Startup.cs

      //注册DbContext  ConfigureServices
                services.AddDbContext<testContext>(options => options.UseMySql(Configuration.GetConnectionString("DefaultConnection")));
    services.AddControllers();
     services.AddScoped<IAlltestitemService, AlltestitemService>();//ioc
    

    四 添加Swagger

    安装依赖包

    Swashbuckle.AspNetCore
    

    ConfigureServices(IServiceCollection services)

    //注册swagger服务
                services.AddSwaggerGen(c =>
                  {
                      c.SwaggerDoc("v1", new OpenApiInfo
                      {
                          Version = "v1",
                          Title = "SN博客 API",
                          Description = "EFCore数据操作 ASP.NET Core Web API",
                          TermsOfService = new Uri("https://example.com/terms"),
                          Contact = new OpenApiContact
                          {
                              Name = "Shayne Boyer",
                              Email = string.Empty,
                              Url = new Uri("https://twitter.com/spboyer"),
                          },
                          License = new OpenApiLicense
                          {
                              Name = "Use under LICX",
                              Url = new Uri("https://example.com/license"),
                          }
                      });
    
                      // 为 Swagger 设置xml文档注释路径
                      var basePath2 = AppContext.BaseDirectory;// xml路径
                      //var xmlModelPath = Path.Combine(basePath2, "Snblog.Enties.xml");//Model层的xml文件名
                      var corePath = Path.Combine(basePath2, "AfterCore.xml");//API层的xml文件名
                      //c.IncludeXmlComments(xmlModelPath);
                      c.IncludeXmlComments(corePath, true);
                      //添加对控制器的标签(描述)
                      c.CustomSchemaIds(type => type.FullName);// 可以解决相同类名会报错的问题
    
                      // c.OperationFilter<AddResponseHeadersFilter>();
                      // c.OperationFilter<AppendAuthorizeToSummaryOperationFilter>();
                      //
                      // c.OperationFilter<SecurityRequirementsOperationFilter>();
    
                  });
    

    Configure(IApplicationBuilder app, IWebHostEnvironment env)

     #region Swagger
                //可以将Swagger的UI页面配置在Configure的开发环境之中
                app.UseSwagger();
                //和Swagger UI
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "SN博客API");
                    c.RoutePrefix = string.Empty;
                });
                #endregion
    

    五 显示层AfterCore

    Controllers->AlltestitemController.cs

    using After.IService;
    using Microsoft.AspNetCore.Mvc;
    using System.Threading.Tasks;
    //默认的约定集将应用于程序集中的所有操作:
    [assembly: ApiConventionType(typeof(DefaultApiConventions))]
    namespace AfterCore.Controllers
    {
        [Route("api/[controller]")]
        [ApiController]
        public class AlltestitemController : ControllerBase
        {
              private readonly IAlltestitemService _service; //IOC依赖注入
    
             public AlltestitemController(IAlltestitemService service)
            {
                _service = service;
            
            }
            /// <summary>
            /// 查询所有
            /// </summary>
            /// <returns></returns>
            // [ApiExplorerSettings(IgnoreApi = true)] //隐藏接口 或者直接对这个方法 private,也可以直接使用obsolete属性
            [HttpGet("GetAllAsync")]
            public async Task<IActionResult> GetAllAsync()
            {
                return Ok(await _service.GetAllAsync());
            }
    
             /// <summary>
            /// 查询总数
            /// </summary>
            /// <returns></returns>
            // [ApiExplorerSettings(IgnoreApi = true)] //隐藏接口 或者直接对这个方法 private,也可以直接使用obsolete属性
            [HttpGet("CountAsync")]
            public async Task<IActionResult> CountAsync()
            {
                return Ok(await _service.CountAsync());
            }
        }
    }
    
    

    运行

  • 相关阅读:
    缓存问题
    基情探测器心得
    新手最常见的误解和错误
    C语言书籍推荐
    初学者编程实战指南 (4) 由一个简单的例子学习抽象
    数据结构的动画演示
    利用IDE使你的代码风格好看一些
    初学者编程实战指南 (2) 避免逻辑的重复
    入门编程语言的选择问题
    关于ACM集训队
  • 原文地址:https://www.cnblogs.com/ouyangkai/p/13921437.html
Copyright © 2011-2022 走看看