NET CORE 3.1 MVC SQL Jackfruit - Net-Core-3-1-Mvc-W-Mysql
NET CORE 3.1 MVC SQL Jackfruit - Net-Core-3-1-Mvc-W-Mysql
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
dotnet add package Pomelo.EntityFrameworkCore.MySql --version 3. Entity Framework Core provider for MySQL
1.1 DB's
These two commands must be run in your project folder every time you set up a new MVC project!
"DBInfo":
{
"Name": "MySQLconnect",
"ConnectionString": "server=localhost;userid=root;password=root;port=3306;databa
=SchemaName;SslMode=None"
}
using Microsoft.EntityFrameworkCore;
services.AddSession();
app.UseSession();
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
}
}
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
}
}
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();
}
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.