D:\dotnet_core\HotelWebMVC\DAL>dotnet ef dbcontext scaffold "Server=192.168.30.110,3306;DataBase=HotelWebDb;User=sa;Pwd=110;" "Pomelo.EntityFrameworkCore.MySql" -s ..\HotelWebMVC\HotelWebMVC.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(); } } } }
业务逻辑部分