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 ProjectDirectoryName |
Creates new dotnet MVC Project for development
|
dotnet add package Pomelo.EntityFrameworkCore.MySql --version 3.1.1 |
Entity Framework Core provider for MySQL DB's
|
dotnet add package Microsoft.EntityFrameworkCore.Design --version 3.1.5 |
EF Core tools for DB creation/migration
|
"DBInfo": |
Add this at the end of your appsettings.json file.
Don't forget a comma before this new section! |
using Microsoft.EntityFrameworkCore; |
Add this to your using statements at the top of Startup.cs
|
services.AddDbContext<MyContext>(options => options.UseMySql (Configuration["DBInfo:ConnectionString"])); |
Add this to the
ConfigureServices method in Startup.cs |
services.AddSession(); |
Add this to the
ConfigureServices method in Startup.cs Adds Session |
app.UseSession(); |
Add this to the
Configure method in Startup.cs. Adds Session |
using Microsoft.EntityFrameworkCore;
namespace ***ProjDirName***.Models
{
public class ***DBContextClassName*** : DbContext
{
public ***DBContextClassName***(DbContextOptions options) : base(options) { }
public DbSet<***ModelName***> ***TableName*** { get; set; }
*Add more tables here*
}
}
using System;
using System.ComponentModel.DataAnnotations;
namespace ***ProjDirName***.Models
{
public class ***ModelName***
{
[Key]
public int ***ModelName***Id { 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()
{
List<***ModelName***> *VariableName* = _context.***TableName***.ToList();
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
|
Updated step 3 to include `using` statement required to set DBContext config
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.