概述
本文简单介绍如何建立ASP.Net Core Web 应用,数据库使用MySQL的方法流程。
1、建立项目,以Student为例
2、选择MVC
3、NuGet包添加Microsoft.EntityFrameworkCore.Tools和Pomelo.EntityFrameworkCore.MySql
4、在Models文件夹下创建Student类
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace StudentApp.Models
{
public class Student
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
}
}
5、在Controller文件夹下右键添加使用EF的MVC控制器
完成之后会自动添加若干文件:
通过观察生成的StudentAppContext.cs 和 StudentController.cs可以看到如何使用数据
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace StudentApp.Models
{
public class StudentAppContext : DbContext
{
public StudentAppContext (DbContextOptions<StudentAppContext> options)
: base(options)
{
}
public DbSet<StudentApp.Models.Student> Student { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using StudentApp.Models;
namespace StudentApp.Controllers
{
public class StudentsController : Controller
{
private readonly StudentAppContext _context;
public StudentsController(StudentAppContext context)
{
_context = context;
}
// GET: Students
public async Task<IActionResult> Index()
{
return View(await _context.Student.ToListAsync());
}
// GET: Students/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var student = await _context.Student
.SingleOrDefaultAsync(m => m.ID == id);
if (student == null)
{
return NotFound();
}
return View(student);
}
// GET: Students/Create
public IActionResult Create()
{
return View();
}
// POST: Students/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ID,Name")] Student student)
{
if (ModelState.IsValid)
{
_context.Add(student);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(student);
}
// GET: Students/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var student = await _context.Student.SingleOrDefaultAsync(m => m.ID == id);
if (student == null)
{
return NotFound();
}
return View(student);
}
// POST: Students/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("ID,Name")] Student student)
{
if (id != student.ID)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(student);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!StudentExists(student.ID))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(student);
}
// GET: Students/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var student = await _context.Student
.SingleOrDefaultAsync(m => m.ID == id);
if (student == null)
{
return NotFound();
}
return View(student);
}
// POST: Students/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var student = await _context.Student.SingleOrDefaultAsync(m => m.ID == id);
_context.Student.Remove(student);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool StudentExists(int id)
{
return _context.Student.Any(e => e.ID == id);
}
}
}
6、打开StartUp.cs,将UseSqlServer该成UseMySql
7、打开appsettings.json,修改数据库连接字符串,该成你的MySQL连接字符串即可
8、打开程序包管理器控制台
9、输入add-migration init,init为此次操作的备注名称,该操作完成后输入update-database更新数据库。
10、打开你的数据库,刷新一下会发现已经大功告成了
11、添加几条数据
12、F5 或 Ctrl+F5 调试或执行程序
至此一切完成!
最后
以上就是犹豫灰狼为你收集整理的ASP.Net Core Web 应用程序 与 MySQL的全部内容,希望文章能够帮你解决ASP.Net Core Web 应用程序 与 MySQL所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复