探索Aspnetcore+mysql+efcore
摘要
之前尝试了,新建asp.net core站点,那么如何和mysql建立连接,如果操作mysql?本篇将尝试使用EntityFrameworkCore进行mysql的操作。
一个例子
首先新建一个空的Asp.net core站点,如图
安装MySql.Data.EntityFrameworkCore
http://www.nuget.org/packages/MySql.Data.EntityFrameworkCore/7.0.5-IR21
安装成功
这时你可以在project.json中看到MySql.Data.EntityFrameworkCore依赖的配置
新建一个User类
public class User { public int Id { set; get; } public string Name { set; get; } public DateTime Dt { set; get; } = DateTime.Now; }
这里使用 db first方式进行数据库的操作,添加一个test的数据库,然后新建一个user表
添加数据库上下文类。
using Microsoft.EntityFrameworkCore; using MySQL.Data.EntityFrameworkCore.Extensions; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Wolfy.EfMySql.Models; namespace Wolfy.EfMySql.Data { public class TestContext : DbContext { public DbSet<User> User { set; get; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseMySQL(@"Server=localhost;database=test;uid=root;pwd=123456"); } }
添加控制器
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Wolfy.EfMySql.Data; using Wolfy.EfMySql.Models; // For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860 namespace Wolfy.EfMySql.Controllers { public class HomeController : Controller { // GET: /<controller>/ public IActionResult Index() { var db = new TestContext(); db.Add(new User { Name = "Hello world" }); db.SaveChanges(); var lst = db.Set<User>().ToList(); return View(lst); } } }
添加路由
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace Wolfy.EfMySql { public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(routes => { routes.MapRoute(name: "default", template: "{controller=Home}/{action=index}"); }); } } }
浏览Index
通过上面,可以看出,是没有添加mvc服务,引起的,修改如下:
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); }
视图
@* For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860 *@ @model List<Wolfy.EfMySql.Models.User> <ul style="list-style-type:none;"> @foreach (var item in Model) { <li>@item.Id @item.Name @item.Dt.ToString("yyyy-MM-dd HH:mm:ss")</li> } </ul>
刷新页面
结语
这里算是尝尝鲜。关于asp.net core的更多内容,需要参考https://www.asp.net/core
-
博客地址:http://www.cnblogs.com/wolf-sun/
博客版权:如果文中有不妥或者错误的地方还望高手的你指出,以免误人子弟。如果觉得本文对你有所帮助不如【推荐】一下!如果你有更好的建议,不如留言一起讨论,共同进步! 再次感谢您耐心的读完本篇文章。
分类:
[Asp.Net Core]
标签:
asp.net core
, mysql
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
2013-12-03 看过《大湿教我写.net通用权限框架(1)之菜单导航篇》之后发生的事(续)——主界面