mysql asp.net core_探索Aspnetcore+mysql+efcore

摘要

之前尝试了,新建asp.net core站点,那么如何和mysql建立连接,如果操作mysql?本篇将尝试使用EntityFrameworkCore进行mysql的操作。

一个例子

首先新建一个空的Asp.net core站点,如图

AAffA0nNPuCLAAAAAElFTkSuQmCC

安装MySql.Data.EntityFrameworkCore

http://www.nuget.org/packages/MySql.Data.EntityFrameworkCore/7.0.5-IR21

AAffA0nNPuCLAAAAAElFTkSuQmCC

安装成功

AAffA0nNPuCLAAAAAElFTkSuQmCC

这时你可以在project.json中看到MySql.Data.EntityFrameworkCore依赖的配置

AAffA0nNPuCLAAAAAElFTkSuQmCC

新建一个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表

AAffA0nNPuCLAAAAAElFTkSuQmCC

添加数据库上下文类。

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 { 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=397860namespace Wolfy.EfMySql.Controllers

{    public class HomeController : Controller

{        // GET: //

public IActionResult Index()

{            var db = new TestContext();

db.Add(new User { Name = "Hello world" });

db.SaveChanges();            var lst = db.Set().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

AAffA0nNPuCLAAAAAElFTkSuQmCC

通过上面,可以看出,是没有添加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

@foreach (var item in Model)

{        

@item.Id   @item.Name   @item.Dt.ToString("yyyy-MM-dd HH:mm:ss")

}

刷新页面

AAffA0nNPuCLAAAAAElFTkSuQmCC

结语

这里算是尝尝鲜。关于asp.net core的更多内容,需要参考https://www.asp.net/core

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值