1. 程式人生 > 其它 >50-10000 study表測試,(資料庫與介面的測試打通) 成功獲取到了本地資料庫資料了!

50-10000 study表測試,(資料庫與介面的測試打通) 成功獲取到了本地資料庫資料了!

資料庫比較簡單的了, 就是建立表就OK了。

entity框架類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// 1 引入空間
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;


namespace Entity
{
    /// <summary>
/// 學習類檔案 study類的建立 /// </summary> public class Study { // 一個類中的內容 包括。 get set訪問器,變數,資料型別 [Key] public int ID { get; set; } public string Code { get; set; } public string RealName { get; set; } public string NickName { get; set; }
public DateTime BillTime { get; set; } public bool Sex { get; set; } } }

邏輯類檔案

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// 1 名稱空間引入
using Entity;

// 這完全是2個類進行關聯起來的。建立獨立的一類檔案,獨立的一個類,就可以是一個類檔案,副檔名為CS的類檔案。
namespace
Business { /// <summary> /// 學習表類 邏輯程式碼寫入 /// </summary> /// // 繼承一個父類. 這裡已經繼承了entity 實體框架類 study類的值內容。可以進行相關需要的操作了。 必須繼承後,才能操作。否則,不能繼續。 // 否則,只能操作當前類。 public class StudyOperate:BaseOperate<Study> { #region linq查詢操作 // 定義獲取分頁資料的方法 public List<Study> GetPage(PageInfo info, string key) { // 變數賦值 var query = GetQueryable(); //if 如果判斷 if (string.IsNullOrEmpty(key) == false) query = query.Where(q => q.NickName.Contains(key)); // 排序 info.RecordCount = query.Count(); query = query .OrderByDescending(q => q.RealName) .ThenByDescending(q => q.BillTime); // 返回 return query.Skip(info.SkipCount) .Take(info.PageSize) .ToList(); } #endregion } }

控制器類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
// 1 名稱空間引入
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Entity;
using Business;

namespace QL.Back.API.Controllers.admin
{
    /// <summary>
    /// 
    /// </summary>
    /// 

    // 2 寫路由
     [Route("api/[controller]/[action]")]
     [EnableCors("any")]

     // 這是一個類,繼續控制器類。 
    public class StudyController:AdminBaseController
    {

        #region

        // 獲取列表。 類中寫獲取列表方法。
         public IActionResult GetList_Study(PageInfo info, string key)
        {

            //上下文類的例項化
            StudyOperate _study = new StudyOperate();

            var list = _study.GetPage(info, key);

            List<Object> result = new List<object>();

            list.ForEach(q => result.Add(new
            {
                q.ID,
                q.Code,
                q.RealName,
                q.NickName,
                q.Sex,
                q.BillTime
            }));
            return JsonList(result, info);



        }


        #endregion


    }
}

效果:

這是從資料庫,建立表,到C#介面方法的定義,整個流程重新走了一遍。 曾經搞好幾天,才搞出來的程式碼,NOW only 用半天不到的time就寫出來了。這就是進步啊!

進步進步進步。 那麼,再去搞連線前端的事情。

日新覆盤:

1 不足的地方,是linq查詢,基礎知識點掌握的不夠好。還是有挺多生疏的地方,需要多多複習。

再有是基礎知識點,類,方法的複習,印象加深。