Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

NET CORE 3.1 MVC SQL Jackfruit - Net-Core-3-1-Mvc-W-Mysql

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

NET CORE 3.1 MVC SQL Jackfruit - Net-Core-3-1-Mvc-W-Mysql

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

.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 Projec​tDi​rec​tor​yN Creates new dotnet MVC Project for develo​pment
ame

Step 2 - Terminal (Inside Project Directory)

dotnet add package Pomelo.En​tit​yFr​ame​wor​kCo​re.M​ySql --version 3. Entity Framework Core provider for MySQL
1.1 DB's

dotnet add package Micros​oft.En​tit​yFr​ame​wor​kCo​re.D​esign --versi EF Core tools for DB creati​on/​mig​ration


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 - appset​tin​gs.json && Startup.cs

"​DBI​nfo​":
{
"​Nam​e": "​MyS​QLc​onn​ect​",
"​Con​nec​tio​nSt​rin​g": "​ser​ver​=lo​cal​hos​t;u​ser​id=​roo​t;p​ass​wor​d=r​oot​;po​rt=​330​6;d​ata​ba
=SchemaName;SslMode=None"
}

using Micros​oft.En​tit​yFr​ame​wor​kCore;

servic​es.A​dd​DbC​ont​ext​<My​Con​tex​t>(​options => option​s.U​seMySql (Confi​gur​ati​on[​"​DBI​nfo​:Co​nne​cti​


S​tri​ng"]));

servic​es.A​dd​Ses​sion();

app.Us​eSe​ssi​on();

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 ProjDi​rName
.Models
{
​ ​ ​ ​public class
DBCont​ext​Cla​ssName
: DbContext
​ ​ ​ {
DBCont​ext​Cla​ssName
​ ​ ​ ​ ​ ​ ​ ​public (DbCon​tex​tOp​tions options) : base(o​ptions) { }
​ ​ ​ ​ ​ ​ ​ ​publicModelName>
DbSet< TableName { get; set; }
​ ​ ​ ​ Add
​ ​ more
​ tables here
​ ​ ​ }
}

Sample code for context model file

Step 5 - Model Example

using System;
using System.Co​mpo​nen​tMo​del.Da​taA​nno​tat​ions;
namespace ProjDi​rName
.Models
{
​ ​ ​ ​public class
ModelName
​ ​ ​ {
​ ​ ​ ​ ​ ​ ​ ​[Key]
ModelNameId
​ ​ ​ ​ ​ ​ ​ ​public int { get; set; }
​ ​ ​ ​ Set
​ ​ all
​ properties here
​ ​ ​ ​ ​ ​ ​ ​public DateTime CreatedAt { get; set; } = DateTi​me.Now;
​ ​ ​ ​ ​ ​ ​ ​public DateTime UpdatedAt { get; set; } = DateTi​me.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.Co​lle​cti​ons.Ge​neric;
using System.Linq;
using Micros​oft.En​tit​yFr​ame​wor​kCore;
using ProjDi​rName
.Models;
public HomeCo​ntr​oller(
Contex​tCl​assName
context)
​ ​ ​ ​ ​ ​ ​ {
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​_co​ntext = context;
​ ​ ​ ​ ​ ​ ​ }
​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​[Ht​tpG​et(​"​")]
​ ​ ​ ​ ​ ​ ​ ​public IActio​nResult Index()
​ ​ ​ ​ ​ ​ ​ {
​ ​ ​ ​ ​ ​ ​ ​ ​ModelName> Variab​leName = _context.TableName.ToList();
​ ​ ​Lis​t<
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​return View();
​ ​ ​ ​ ​ ​ ​ }

Sample of the code required to make DB Context available in contro​llers

Step 7 - Terminal - Migration

dotnet ef migrations add Migrat​ionName Creates a migration in prepar​ation for Creati​ng/​Upd​ating 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, prefer​ably, after all models and relati​onships 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

You might also like