当前位置:Gxlcms > 数据库问题 > 探索Aspnetcore+mysql+efcore

探索Aspnetcore+mysql+efcore

时间:2021-07-01 10:21:17 帮助过:16人阅读

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

探索Aspnetcore+mysql+efcore

标签:time   route   task   err   end   startup   mic   under   dev   

人气教程排行