.Net Core 3.
1 MVC w/MySQL Cheat Sheet
by B C Williams | Dev (Jackfruit) via cheatography.com/146421/cs/31670/
Step 1 - Terminal
dotnet tool install --global dotnet-ef You will only ever need to run this once as it is a global install not project
specific
dotnet new mvc --no-https -o ProjectDirectoryN Creates new dotnet MVC Project for development
ame
Step 2 - Terminal (Inside Project Directory)
dotnet add package Pomelo.EntityFrameworkCore.MySql --version 3. Entity Framework Core provider for MySQL
1.1 DB's
dotnet add package Microsoft.EntityFrameworkCore.Design --versi EF Core tools for DB creation/migration
on 3.1.5
These two commands must be run in your project folder every time you set up a new MVC project!
Step 3 - appsettings.json && Startup.cs
"DBInfo":
{
"Name": "MySQLconnect",
"ConnectionString": "server=localhost;userid=root;password=root;port=3306;databa
=SchemaName;SslMode=None"
}
using Microsoft.EntityFrameworkCore;
services.AddDbContext<MyContext>(options => options.UseMySql (Configuration["DBInfo:Connecti
String"]));
services.AddSession();
app.UseSession();
By B C Williams | Dev Published 14th April, 2022. Sponsored by Readable.com
(Jackfruit) Last updated 17th April, 2022. Measure your website readability!
cheatography.com/jackfruit/ Page 1 of 3. https://readable.com
.Net Core 3.1 MVC w/MySQL Cheat Sheet
by B C Williams | Dev (Jackfruit) via cheatography.com/146421/cs/31670/
Step 4 - Context File
using Microsoft.EntityFrameworkCore;
namespace ProjDirName
.Models
{
public class
DBContextClassName
: DbContext
{
DBContextClassName
public (DbContextOptions options) : base(options) { }
publicModelName>
DbSet< TableName { get; set; }
Add
more
tables here
}
}
Sample code for context model file
Step 5 - Model Example
using System;
using System.ComponentModel.DataAnnotations;
namespace ProjDirName
.Models
{
public class
ModelName
{
[Key]
ModelNameId
public int { get; set; }
Set
all
properties here
public DateTime CreatedAt { get; set; } = DateTime.Now;
public DateTime UpdatedAt { get; set; } = DateTime.Now;
Set
Navigation
Properties
}
}
Class model for database table
By B C Williams | Dev Published 14th April, 2022. Sponsored by Readable.com
(Jackfruit) Last updated 17th April, 2022. Measure your website readability!
cheatography.com/jackfruit/ Page 2 of 3. https://readable.com
.Net Core 3.1 MVC w/MySQL Cheat Sheet
by B C Williams | Dev (Jackfruit) via cheatography.com/146421/cs/31670/
Step 6 - Context Dependency Injection
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using ProjDirName
.Models;
public HomeController(
ContextClassName
context)
{
_context = context;
}
[HttpGet("")]
public IActionResult Index()
{
ModelName> VariableName = _context.TableName.ToList();
List<
return View();
}
Sample of the code required to make DB Context available in controllers
Step 7 - Terminal - Migration
dotnet ef migrations add MigrationName Creates a migration in preparation for Creating/Updating your DB Schema
dotnet ef database update Updates Database schema with most recent migration
This step to be done after all previous steps are complete and, preferably, after all models and relationships are in place.
By B C Williams | Dev Published 14th April, 2022. Sponsored by Readable.com
(Jackfruit) Last updated 17th April, 2022. Measure your website readability!
cheatography.com/jackfruit/ Page 3 of 3. https://readable.com