基于.NET Core 3.1 网站开发和部署的方法
using System; using System.Linq; using System.Collections.Generic; using Models; namespace DAL { public class NewsService { private EFCoreHelper helper = new EFCoreHelper(new HotelWebDbContext()); /// <summary> /// 添加新闻 /// </summary> /// <param></param> /// <returns></returns> public int AddNews(News news) => helper.Add(news); /// <summary> /// 修改新闻 /// </summary> /// <param></param> /// <returns></returns> public int ModifyNews(News news) => helper.Modify(news); /// <summary> /// 删除新闻 /// </summary> /// <param></param> /// <returns></returns> public int DeleteNews(string newssId) { News news = new News() { Id = Convert.ToUInt32(newssId) }; return helper.Delete(news); } /// <summary> /// 获取指定数量的新闻列表 /// </summary> /// <param></param> /// <returns></returns> public List<News> GetNews(int count) { using (HotelWebDbContext dbContext = new HotelWebDbContext()) { return (from n in dbContext.News orderby n.PublishTime descending select n).Take(count).ToList(); } } /// <summary> /// 根据ID获取新闻信息 /// </summary> /// <param></param> /// <returns></returns> public News GetNewsById(string newssId) { uint id = Convert.ToUInt32(newssId); using (HotelWebDbContext dbContext = new HotelWebDbContext()) { return (from n in dbContext.News where n.Id == id select n).FirstOrDefault(); } } /// <summary> /// 获取所有的新闻分类 /// </summary> /// <returns></returns> public List<NewsCategory> GetCategories() { using (HotelWebDbContext dbContext = new HotelWebDbContext()) { return (from c in dbContext.NewsCategory select c).ToList(); } } } } 业务逻辑部分 using System.Collections.Generic; using DAL; using Models; namespace BLL { public class NewsManager { private NewsService objService=new NewsService(); /// <summary> /// 添加新闻 /// </summary> /// <param></param> /// <returns></returns> public int AddNews(News news) => objService.AddNews(news); /// <summary> /// 修改新闻 /// </summary> /// <param></param> /// <returns></returns> public int ModifyNews(News news) => objService.ModifyNews(news); /// <summary> /// 删除新闻 /// </summary> /// <param></param> /// <returns></returns> public int DeleteNews(string newssId) => objService.DeleteNews(newssId); /// <summary> /// 获取指定数量的新闻列表 /// </summary> /// <param></param> /// <returns></returns> public List<News> GetNews(int count) => objService.GetNews(count); /// <summary> /// 根据ID获取新闻信息 /// </summary> /// <param></param> /// <returns></returns> public News GetNewsById(string newssId) => objService.GetNewsById(newssId); /// <summary> /// 获取所有的新闻分类 /// </summary> /// <returns></returns> public List<NewsCategory> GetCategories() => objService.GetCategories(); } } 3.添加一个控制台项目用来测试 添加需要的引用 dotnet add package Microsoft.EntityFrameworkCore.Design dotnet add package Pomelo.EntityFrameworkCore.MySql DbContext中的数据库连接字符串添加回去 (编辑:晋中站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |