.Net 6 Context独立至ClassLibrary

延续 .NET 6 WEB API 如何安装DB First
如何将EninyFrameWork 独立至一个专案,
而不需透过Controller 去得到Contenxt

1.在UserManageRepository专案加入BuilderExtender.cs

using UserManageRepository.Context;using Microsoft.AspNetCore.Builder;using Microsoft.Extensions.DependencyInjection;namespace UserManageRepository{    public class BuilderExtender    {        public static void AddDbContexts(WebApplicationBuilder? builder)        {            builder.Services.AddDbContext<dbCustomDbSampleContext>();        }    }}

2.在UserManageRepository专案加入cs
这个.cs是用来做Models做的增删茶改等动作,
并且在此引入contenxt的 这样就不需要再cotroller引入Context了

(1)IUserRepositoryEnity.cs

using Microsoft.EntityFrameworkCore;using UserManageRepository.Context;using UserManageRepository.Interfaces;using UserManageRepository.Models.Data;namespace UserManageRepository.Repository{    public class UserRepositoryEnity: IUserRepositoryEnity    {        private readonly dbCustomDbSampleContext _dbCustomDbSampleContext;        public UserRepositoryEnity(dbCustomDbSampleContext context) {            _dbCustomDbSampleContext = context;        }        public async Task<IList<User>> GetUser()        {            return await _dbCustomDbSampleContext.Users.ToListAsync();        }    }}

(2)IUserRepositoryEnity.cs
=>这个Interface因为继承了UserRepositoryEnity,所以后续再controller只要用呼叫就可以取得 他所队的Repository了

using UserManageRepository.Models.Data;namespace UserManageRepository.Interfaces{    public interface IUserRepositoryEnity    {        Task<IList<User>> GetUser();    }}

3.在Program.cs 中的加入

builder.Services.AddScoped<IUserRepositoryEnity, UserRepositoryEnity>();//这边怎样把 DbContext 移到 RepositoryBuilderExtender.AddDbContexts(builder);//将注册地方更改为UserManageRepository专案

範例如下

using UserManageRepository.Interfaces;using UserManageRepository.Repository;using UserManageRepository;var builder = WebApplication.CreateBuilder(args);// Add services to the container.builder.Services.AddControllers();// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbucklebuilder.Services.AddEndpointsApiExplorer();builder.Services.AddSwaggerGen();builder.Services.AddScoped<IUserRepositoryEnity, UserRepositoryEnity>();//这边怎样把 DbContext 移到 RepositoryBuilderExtender.AddDbContexts(builder);//将注册地方更改为UserManageRepository专案var app = builder.Build();// Configure the HTTP request pipeline.if (app.Environment.IsDevelopment()){    app.UseSwagger();    app.UseSwaggerUI();}app.UseHttpsRedirection();app.UseAuthorization();app.MapControllers();app.Run();

完成 如此便可在Controller较用

4.Controller叫用

using Microsoft.AspNetCore.Mvc;using UserManageRepository.Context;using UserManageRepository.Interfaces;namespace PJUserManage.Controllers{    [ApiController]    [Route("[controller]")]    public class UserManageController : ControllerBase    {        private readonly IUserRepositoryEnity _IUserRepository;        public UserManageController(dbCustomDbSampleContext context, IUserRepositoryEnity users)        {            _IUserRepository = users;        }        [HttpGet]        public async Task<IActionResult> GetUser()        {            return Ok(await _IUserRepository.GetUser());        }    }}

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章