基于.NET Core 3.1 网站开发和部署的方法
D:dotnet_coreHotelWebMVCDAL>dotnet ef dbcontext scaffold "Server=192.168.30.110,3306;DataBase=HotelWebDb;User=sa;Pwd=110;" "Pomelo.EntityFrameworkCore.MySql" -s ..HotelWebMVCHotelWebMVC.csproj Build started... Build succeeded.
修改连接字符串的位置 修改在appsettings.json文件中添加连接字符串 { "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*", "ConnectionStrings":{ "HotelWeb":"Server=192.168.30.110,3306;DataBase=HotelWebDb;User=sa;Pwd=110;" } } 然后在Sartup.cs文件获取连接字符串 public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); string connString=Configuration.GetConnectionString("HotelWeb"); services.AddDbContext<DAL.HotelWebDbContext>(options=>options.UseMySql(connString, x => x.ServerVersion("5.5.64-mariadb"))); } 最后DbContenxt类中配置就可以删除了 移动DAL生成的实体类到Models模块 ② 另一种使用ADO.NET实现 不是这次的重点,所以略过。 三、编写Model部分的代码1.编写一个简单的Helper类 using Microsoft.EntityFrameworkCore; namespace DAL { class EFCoreHelper { private DbContext dbContext = null; public EFCoreHelper(DbContext context) { this.dbContext = context; } /// <summary> /// 添加实体 /// </summary> /// <typeparam></typeparam> public int Add<T>(T entity) where T : class { dbContext.Entry(entity).State=EntityState.Added; return dbContext.SaveChanges(); } /// <summary> /// 修改实体的全部属性 /// </summary> /// <typeparam></typeparam> public int Modify<T>(T entity) where T:class { dbContext.Entry(entity).State=EntityState.Modified; return dbContext.SaveChanges(); } /// <summary> /// 删除实体 /// </summary> /// <typeparam></typeparam> public int Delete<T>(T entity) where T:class { dbContext.Entry(entity).State=EntityState.Deleted; return dbContext.SaveChanges(); } } } 2.完成新闻后台数据访问类 数据访问类 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中的数据库连接字符串添加回去 (编辑:焦作站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |