zoukankan      html  css  js  c++  java
  • Intern Day79

    在xxx.Controllers文件中实现相关接口,增删改查操作放在这里面。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.AspNetCore.Components.Web;
    using Microsoft.AspNetCore.Mvc;
    
    namespace Demo_One.Controllers
    {
        [ApiController]
        [Route("api/patient")]
        public class PatientController:ControllerBase
        {
            
            private readonly PatientDbContext _patientDbContext;
            public PatientController(PatientDbContext patientDbContext) // 构造函数注入
            {
                _patientDbContext = patientDbContext;
            }
            
    
            // 增加
            [HttpPost("add-patient")]
            public Patient Add(Patient patient)
            {
                _patientDbContext.Add(patient); 
                _patientDbContext.SaveChanges(); 
                return patient;
            }
            // 比如 下面同理
            public async Task SavebbbAsync(Patient input)
            {
                _patientDbContext.Add(patient); 
                _patientDbContext.SaveChanges(); 
                return patient;
                
                var aaa = new xx.xx.xx
                {
                    StudyId = input.StudyId,
                    xxx,
                    xxx
                }
                await _unitOfWork.DbContext.bbb.AddAsync(aaa);
                _unitOfWork.SaveChanges();
            }
    
            
            // 删除
            [HttpDelete("delete-patient")]
            public void Delete(int id) 
            {
                _patientDbContext.Remove(id); 
                _patientDbContext.SaveChanges(); 
            }
            // 比如
            
            
            // 修改/更新
            [HttpPut("update-info")]
            public bool Update(Patient patient) 
            {
                _patientDbContext.Update(patient);
                _patientDbContext.SaveChanges();
                return true; 
            }
            // 比如
            
            
            [HttpGet("get-patient-details")]
            public Patient GetDetail(int id) // 获取一个病人的详细信息
            {
                return _patientDbContext.Patients.Find(id);
            } 
            // 比如
            
            
            // 分页
            [HttpGet("get-patient-list")]
            public List<Patient> list(string keyword, int pageIndex, int pageSize)
            {
                var skip=pageSize*(pageIndex-1);
                var patient = _patientDbContext.Patients
                    .Where(it=>it.Name.Contains(keyword)) 
                    .Skip(skip) 
                    .Take(pageSize) 
                    .ToList();
                return patient;
            }
        }
    }
    

    补充文档

    1. 微软官方文档:https://docs.microsoft.com/zh-cn/ef/core/querying/

    2. 博客:https://blog.csdn.net/qq_43024228/article/details/108630518

  • 相关阅读:
    [转载]初学C#之list
    List<>过滤重复的简单方法
    C# List<> 删除
    C# 生成随机字符串
    C#正则表达式之字符替换
    c#中怎么删除一个非空目录
    treeview 点击时选中节点
    教程链接
    iOS 允许后台任务吗?
    Git Add,Git别名等
  • 原文地址:https://www.cnblogs.com/OFSHK/p/14742087.html
Copyright © 2011-2022 走看看