Skip to content

Commit c7e74a2

Browse files
committed
Database GET
Session 3 Database CRUD - GET
1 parent 2b1f5c9 commit c7e74a2

15 files changed

Lines changed: 339 additions & 8 deletions

.DS_Store

0 Bytes
Binary file not shown.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,5 @@ FodyWeavers.xsd
396396

397397
# JetBrains Rider
398398
*.sln.iml
399+
.DS_Store
400+
.DS_Store

CMS.csproj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net7.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
</PropertyGroup>
@@ -13,11 +13,20 @@
1313
</PropertyGroup>
1414
<ItemGroup>
1515
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="7.0.10" />
16+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.10" />
17+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.10" />
18+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.10">
19+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
20+
<PrivateAssets>all</PrivateAssets>
21+
</PackageReference>
22+
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0" />
1623
</ItemGroup>
1724
<ItemGroup>
1825
<None Remove="Views\Record\" />
26+
<None Remove="Data\" />
1927
</ItemGroup>
2028
<ItemGroup>
2129
<Folder Include="Views\Record\" />
30+
<Folder Include="Data\" />
2231
</ItemGroup>
2332
</Project>

Controllers/RecordController.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Threading.Tasks;
5+
using CMS.Data;
6+
using CMS.Models;
57
using Microsoft.AspNetCore.Mvc;
68

79
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
@@ -10,6 +12,12 @@ namespace CMS.Controllers
1012
{
1113
public class RecordController : Controller
1214
{
15+
private readonly ClassDatabaseContext _db;
16+
public RecordController(ClassDatabaseContext db)
17+
{
18+
_db = db;
19+
}
20+
1321
// GET: /<controller>/
1422
public IActionResult Index()
1523
{
@@ -18,7 +26,9 @@ public IActionResult Index()
1826

1927
public IActionResult TimeTable()
2028
{
21-
return View();
29+
List<Classes> objClassList = _db.Classes.ToList();
30+
Console.Write(objClassList[0].className);
31+
return View(objClassList);
2232
}
2333

2434
public IActionResult Assessments()

Data/ClassDatabaseContext.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using CMS.Models;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace CMS.Data
5+
{
6+
public class ClassDatabaseContext : DbContext
7+
{
8+
public ClassDatabaseContext(DbContextOptions<ClassDatabaseContext> options) : base(options)
9+
{
10+
}
11+
12+
public DbSet<Classes> Classes { get; set; }
13+
14+
protected override void OnModelCreating(ModelBuilder modelBuilder)
15+
{
16+
modelBuilder.Entity<Classes>().HasData(
17+
new Classes { ID = 1, period = 1, className = "Physcis" },
18+
new Classes { ID = 2, period = 2, className = "Literature" }
19+
);
20+
}
21+
}
22+
}

Migrations/20230918124802_InitialCreate.Designer.cs

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Microsoft.EntityFrameworkCore.Metadata;
2+
using Microsoft.EntityFrameworkCore.Migrations;
3+
4+
#nullable disable
5+
6+
namespace CMS.Migrations
7+
{
8+
/// <inheritdoc />
9+
public partial class InitialCreate : Migration
10+
{
11+
/// <inheritdoc />
12+
protected override void Up(MigrationBuilder migrationBuilder)
13+
{
14+
migrationBuilder.AlterDatabase()
15+
.Annotation("MySql:CharSet", "utf8mb4");
16+
17+
migrationBuilder.CreateTable(
18+
name: "Classes",
19+
columns: table => new
20+
{
21+
ID = table.Column<int>(type: "int", nullable: false)
22+
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
23+
period = table.Column<int>(type: "int", nullable: false),
24+
className = table.Column<string>(type: "longtext", nullable: false)
25+
.Annotation("MySql:CharSet", "utf8mb4")
26+
},
27+
constraints: table =>
28+
{
29+
table.PrimaryKey("PK_Classes", x => x.ID);
30+
})
31+
.Annotation("MySql:CharSet", "utf8mb4");
32+
}
33+
34+
/// <inheritdoc />
35+
protected override void Down(MigrationBuilder migrationBuilder)
36+
{
37+
migrationBuilder.DropTable(
38+
name: "Classes");
39+
}
40+
}
41+
}

Migrations/20230918125811_AddingData.Designer.cs

Lines changed: 58 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
#nullable disable
4+
5+
#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
6+
7+
namespace CMS.Migrations
8+
{
9+
/// <inheritdoc />
10+
public partial class AddingData : Migration
11+
{
12+
/// <inheritdoc />
13+
protected override void Up(MigrationBuilder migrationBuilder)
14+
{
15+
migrationBuilder.InsertData(
16+
table: "Classes",
17+
columns: new[] { "ID", "className", "period" },
18+
values: new object[,]
19+
{
20+
{ 1, "Physcis", 1 },
21+
{ 2, "Literature", 2 }
22+
});
23+
}
24+
25+
/// <inheritdoc />
26+
protected override void Down(MigrationBuilder migrationBuilder)
27+
{
28+
migrationBuilder.DeleteData(
29+
table: "Classes",
30+
keyColumn: "ID",
31+
keyValue: 1);
32+
33+
migrationBuilder.DeleteData(
34+
table: "Classes",
35+
keyColumn: "ID",
36+
keyValue: 2);
37+
}
38+
}
39+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// <auto-generated />
2+
using CMS.Data;
3+
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.EntityFrameworkCore.Infrastructure;
5+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
6+
7+
#nullable disable
8+
9+
namespace CMS.Migrations
10+
{
11+
[DbContext(typeof(ClassDatabaseContext))]
12+
partial class ClassDatabaseContextModelSnapshot : ModelSnapshot
13+
{
14+
protected override void BuildModel(ModelBuilder modelBuilder)
15+
{
16+
#pragma warning disable 612, 618
17+
modelBuilder
18+
.HasAnnotation("ProductVersion", "7.0.10")
19+
.HasAnnotation("Relational:MaxIdentifierLength", 64);
20+
21+
modelBuilder.Entity("CMS.Models.Classes", b =>
22+
{
23+
b.Property<int>("ID")
24+
.ValueGeneratedOnAdd()
25+
.HasColumnType("int");
26+
27+
b.Property<string>("className")
28+
.IsRequired()
29+
.HasColumnType("longtext");
30+
31+
b.Property<int>("period")
32+
.HasColumnType("int");
33+
34+
b.HasKey("ID");
35+
36+
b.ToTable("Classes");
37+
38+
b.HasData(
39+
new
40+
{
41+
ID = 1,
42+
className = "Physcis",
43+
period = 1
44+
},
45+
new
46+
{
47+
ID = 2,
48+
className = "Literature",
49+
period = 2
50+
});
51+
});
52+
#pragma warning restore 612, 618
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)