1 增加组织model
lsbzdw组织
public class lsbzdw
{
[Key]
public int lsbzdw_Id { get; set; }
public string lsbzdw_Code { get; set; }
public string lsbzdw_Name { get; set; }
public DateTime lsbzdw_CreateTime { get; set; }
}
2 增加datadb
用于处理与数据的连接,数据的保存获取操作
public class ScmCoreDbContext:DbContext
{
public ScmCoreDbContext(DbContextOptions options) : base(options) { }
public DbSet<lsbzdw> lsbzdw { get; set; }
}
3 使用sqlite保存数据
nuget获取sqlite包以及ef包
services中增加db的DI引入处理
services.AddDbContext<ScmCoreDbContext>(options => { options.UseSqlite("datasource=scm.db"); });
4 创建IEntityBaseRepository
实现获取数据、增加数据、获取单个数据的功能
public interface IEntityBaseRepository<T> where T:class
{
IQueryable<T> GetAll();
Task<T> GetById(int Id);
void Add(T t);
}
增加lsbzdwRepository实现
public class lszbdwRepository : IEntityBaseRepository<lsbzdw>
{
private readonly ScmCoreDbContext _scmCoreDbContext;
public lszbdwRepository(ScmCoreDbContext scmCoreDbContext)
{
_scmCoreDbContext = scmCoreDbContext;
}
public void Add(lsbzdw t)
{
_scmCoreDbContext.lsbzdw.Add(t);
_scmCoreDbContext.SaveChanges();
}
public IQueryable<lsbzdw> GetAll()
{
return _scmCoreDbContext.lsbzdw.AsQueryable();
}
public async Task<lsbzdw> GetById(int Id)
{
return await _scmCoreDbContext.lsbzdw.FirstOrDefaultAsync(x => x.lsbzdw_Id == Id);
}
}
5 创建lsbzdwcontroller控制器
public class lsbzdwController:ControllerBase
{
private readonly IEntityBaseRepository<lsbzdw> entityBaseRepository;
private readonly IMapper _mapper;
public lsbzdwController(IEntityBaseRepository<lsbzdw> iEntityBaseRepository,
IMapper mapper)
{
entityBaseRepository = iEntityBaseRepository;
_mapper = mapper;
}
[HttpGet]
public async Task<List<lsbzdwViewModel>> GetAll()
{
var r = await entityBaseRepository.GetAll().ToListAsync();
List<lsbzdwViewModel> res = _mapper.Map<List<lsbzdwViewModel>>(r);
return res;
}
[HttpGet]
[Route("{id}")]
public async Task<lsbzdwViewModel> GetById(int Id)
{
var r = await entityBaseRepository.GetById(Id);
var res = _mapper.Map<lsbzdwViewModel>(r);
return res;
}
[HttpPost]
public void Save(lsbzdwAddViewModel addView)
{
lsbzdw saveInfo=new lsbzdw();
saveInfo.lsbzdw_Code = addView.Code;
saveInfo.lsbzdw_Name = addView.Name;
saveInfo.lsbzdw_CreateTime=DateTime.Now;
entityBaseRepository.Add(saveInfo);
}
}
由于我处理了返回数的类型,所以也得需要增加viewModel
用于保存的模型
public class lsbzdwAddViewModel
{
public string Code { get; set; }
public string Name { get; set; }
}
用于显示的模型结构
public class lsbzdwViewModel
{
public int Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public DateTime CreateTime { get; set; }
}
6 mapper映射
由于存在不同的结构,需要设置mapper映射。
public class MapperFile:Profile
{
public MapperFile()
{
CreateMap<lsbzdw, lsbzdwViewModel>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.lsbzdw_Id))
.ForMember(dest => dest.Code, opt => opt.MapFrom(src => src.lsbzdw_Code))
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.lsbzdw_Name))
.ForMember(dest => dest.CreateTime, opt => opt.MapFrom(src => src.lsbzdw_CreateTime));
}
}
services增加mapper文件处理
可以这样写:
var mappingConfig=new MapperConfiguration(mc =>
{
mc.AddProfile(new MapperFile.MapperFile());
});
IMapper mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
也可以这样写:
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
7 增加跨域处理
跨域处理是为了保证angualr访问netcore后端时可以正常访问。
services.AddCors(options => options.AddPolicy("CorsPolicy",
builder =>
{
builder.AllowAnyMethod()
.AllowAnyHeader()
.AllowAnyOrigin();
}));
在configure中增加使用跨域处理
app.UseCors("CorsPolicy");
在控制器上增加
[EnableCors("CorsPolicy")]
[ApiController]
[Route("api/lsbzdw")]
public class lsbzdwController:ControllerBase
8 验证
可以在Postman中测试api是否正常