From 62385faa76def4caed9f69d07ddb0821c8c8dfa5 Mon Sep 17 00:00:00 2001
From: Manuel Thalmann <m@nuth.ch>
Date: Thu, 6 Jun 2024 21:09:24 +0200
Subject: [PATCH] Create an EF project in .NET Core

---
 .../EntityFramework/EFCore/BloggingContext.cs | 38 +++++++++
 .../Lab9/EntityFramework/EFCore/EFCore.csproj | 18 ++++
 .../20240606190606_InitialCreate.Designer.cs  | 85 +++++++++++++++++++
 .../20240606190606_InitialCreate.cs           | 63 ++++++++++++++
 .../BloggingContextModelSnapshot.cs           | 82 ++++++++++++++++++
 Tasks/Lab9/EntityFramework/EFCore/Program.cs  | 12 +++
 .../Lab9/EntityFramework/EntityFramework.sln  |  6 ++
 7 files changed, 304 insertions(+)
 create mode 100644 Tasks/Lab9/EntityFramework/EFCore/BloggingContext.cs
 create mode 100644 Tasks/Lab9/EntityFramework/EFCore/EFCore.csproj
 create mode 100644 Tasks/Lab9/EntityFramework/EFCore/Migrations/20240606190606_InitialCreate.Designer.cs
 create mode 100644 Tasks/Lab9/EntityFramework/EFCore/Migrations/20240606190606_InitialCreate.cs
 create mode 100644 Tasks/Lab9/EntityFramework/EFCore/Migrations/BloggingContextModelSnapshot.cs
 create mode 100644 Tasks/Lab9/EntityFramework/EFCore/Program.cs

diff --git a/Tasks/Lab9/EntityFramework/EFCore/BloggingContext.cs b/Tasks/Lab9/EntityFramework/EFCore/BloggingContext.cs
new file mode 100644
index 0000000..79128a0
--- /dev/null
+++ b/Tasks/Lab9/EntityFramework/EFCore/BloggingContext.cs
@@ -0,0 +1,38 @@
+using Microsoft.EntityFrameworkCore;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace EFCore
+{
+    public class BloggingContext : DbContext
+    {
+        DbSet<MealType> MealTypes { get; set; }
+        public DbSet<Meal> Meals { get; set; }
+
+        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
+        {
+            base.OnConfiguring(optionsBuilder);
+            optionsBuilder.UseSqlServer(@"Server=(local);initial catalog=FlughafenDB;integrated security=true;Trusted_Connection=True;TrustServerCertificate=True");
+        }
+    }
+
+    public class MealType
+    {
+        public int MealTypeId { get; set; }
+        public string? Description { get; set; }
+        public List<Meal> Meals { get; } = new();
+    }
+
+    public class Meal
+    {
+        public int MealId { get; set; }
+        public string? MealName { get; set; }
+        public string? MealDescription { get; set; }
+
+        public int MealTypeId { get; set; }
+        public MealType? MealType { get; set; }
+    }
+}
diff --git a/Tasks/Lab9/EntityFramework/EFCore/EFCore.csproj b/Tasks/Lab9/EntityFramework/EFCore/EFCore.csproj
new file mode 100644
index 0000000..fb9c185
--- /dev/null
+++ b/Tasks/Lab9/EntityFramework/EFCore/EFCore.csproj
@@ -0,0 +1,18 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>net8.0</TargetFramework>
+    <ImplicitUsings>enable</ImplicitUsings>
+    <Nullable>enable</Nullable>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.6" />
+    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.6">
+      <PrivateAssets>all</PrivateAssets>
+      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+    </PackageReference>
+  </ItemGroup>
+
+</Project>
diff --git a/Tasks/Lab9/EntityFramework/EFCore/Migrations/20240606190606_InitialCreate.Designer.cs b/Tasks/Lab9/EntityFramework/EFCore/Migrations/20240606190606_InitialCreate.Designer.cs
new file mode 100644
index 0000000..a13886e
--- /dev/null
+++ b/Tasks/Lab9/EntityFramework/EFCore/Migrations/20240606190606_InitialCreate.Designer.cs
@@ -0,0 +1,85 @@
+// <auto-generated />
+using EFCore;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Metadata;
+using Microsoft.EntityFrameworkCore.Migrations;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+
+#nullable disable
+
+namespace EFCore.Migrations
+{
+    [DbContext(typeof(BloggingContext))]
+    [Migration("20240606190606_InitialCreate")]
+    partial class InitialCreate
+    {
+        /// <inheritdoc />
+        protected override void BuildTargetModel(ModelBuilder modelBuilder)
+        {
+#pragma warning disable 612, 618
+            modelBuilder
+                .HasAnnotation("ProductVersion", "8.0.6")
+                .HasAnnotation("Relational:MaxIdentifierLength", 128);
+
+            SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
+
+            modelBuilder.Entity("EFCore.Meal", b =>
+                {
+                    b.Property<int>("MealId")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("int");
+
+                    SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("MealId"));
+
+                    b.Property<string>("MealDescription")
+                        .HasColumnType("nvarchar(max)");
+
+                    b.Property<string>("MealName")
+                        .HasColumnType("nvarchar(max)");
+
+                    b.Property<int>("MealTypeId")
+                        .HasColumnType("int");
+
+                    b.HasKey("MealId");
+
+                    b.HasIndex("MealTypeId");
+
+                    b.ToTable("Meals");
+                });
+
+            modelBuilder.Entity("EFCore.MealType", b =>
+                {
+                    b.Property<int>("MealTypeId")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("int");
+
+                    SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("MealTypeId"));
+
+                    b.Property<string>("Description")
+                        .HasColumnType("nvarchar(max)");
+
+                    b.HasKey("MealTypeId");
+
+                    b.ToTable("MealTypes");
+                });
+
+            modelBuilder.Entity("EFCore.Meal", b =>
+                {
+                    b.HasOne("EFCore.MealType", "MealType")
+                        .WithMany("Meals")
+                        .HasForeignKey("MealTypeId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
+                    b.Navigation("MealType");
+                });
+
+            modelBuilder.Entity("EFCore.MealType", b =>
+                {
+                    b.Navigation("Meals");
+                });
+#pragma warning restore 612, 618
+        }
+    }
+}
diff --git a/Tasks/Lab9/EntityFramework/EFCore/Migrations/20240606190606_InitialCreate.cs b/Tasks/Lab9/EntityFramework/EFCore/Migrations/20240606190606_InitialCreate.cs
new file mode 100644
index 0000000..a781680
--- /dev/null
+++ b/Tasks/Lab9/EntityFramework/EFCore/Migrations/20240606190606_InitialCreate.cs
@@ -0,0 +1,63 @@
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+namespace EFCore.Migrations
+{
+    /// <inheritdoc />
+    public partial class InitialCreate : Migration
+    {
+        /// <inheritdoc />
+        protected override void Up(MigrationBuilder migrationBuilder)
+        {
+            migrationBuilder.CreateTable(
+                name: "MealTypes",
+                columns: table => new
+                {
+                    MealTypeId = table.Column<int>(type: "int", nullable: false)
+                        .Annotation("SqlServer:Identity", "1, 1"),
+                    Description = table.Column<string>(type: "nvarchar(max)", nullable: true)
+                },
+                constraints: table =>
+                {
+                    table.PrimaryKey("PK_MealTypes", x => x.MealTypeId);
+                });
+
+            migrationBuilder.CreateTable(
+                name: "Meals",
+                columns: table => new
+                {
+                    MealId = table.Column<int>(type: "int", nullable: false)
+                        .Annotation("SqlServer:Identity", "1, 1"),
+                    MealName = table.Column<string>(type: "nvarchar(max)", nullable: true),
+                    MealDescription = table.Column<string>(type: "nvarchar(max)", nullable: true),
+                    MealTypeId = table.Column<int>(type: "int", nullable: false)
+                },
+                constraints: table =>
+                {
+                    table.PrimaryKey("PK_Meals", x => x.MealId);
+                    table.ForeignKey(
+                        name: "FK_Meals_MealTypes_MealTypeId",
+                        column: x => x.MealTypeId,
+                        principalTable: "MealTypes",
+                        principalColumn: "MealTypeId",
+                        onDelete: ReferentialAction.Cascade);
+                });
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Meals_MealTypeId",
+                table: "Meals",
+                column: "MealTypeId");
+        }
+
+        /// <inheritdoc />
+        protected override void Down(MigrationBuilder migrationBuilder)
+        {
+            migrationBuilder.DropTable(
+                name: "Meals");
+
+            migrationBuilder.DropTable(
+                name: "MealTypes");
+        }
+    }
+}
diff --git a/Tasks/Lab9/EntityFramework/EFCore/Migrations/BloggingContextModelSnapshot.cs b/Tasks/Lab9/EntityFramework/EFCore/Migrations/BloggingContextModelSnapshot.cs
new file mode 100644
index 0000000..d9cf027
--- /dev/null
+++ b/Tasks/Lab9/EntityFramework/EFCore/Migrations/BloggingContextModelSnapshot.cs
@@ -0,0 +1,82 @@
+// <auto-generated />
+using EFCore;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Metadata;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+
+#nullable disable
+
+namespace EFCore.Migrations
+{
+    [DbContext(typeof(BloggingContext))]
+    partial class BloggingContextModelSnapshot : ModelSnapshot
+    {
+        protected override void BuildModel(ModelBuilder modelBuilder)
+        {
+#pragma warning disable 612, 618
+            modelBuilder
+                .HasAnnotation("ProductVersion", "8.0.6")
+                .HasAnnotation("Relational:MaxIdentifierLength", 128);
+
+            SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
+
+            modelBuilder.Entity("EFCore.Meal", b =>
+                {
+                    b.Property<int>("MealId")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("int");
+
+                    SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("MealId"));
+
+                    b.Property<string>("MealDescription")
+                        .HasColumnType("nvarchar(max)");
+
+                    b.Property<string>("MealName")
+                        .HasColumnType("nvarchar(max)");
+
+                    b.Property<int>("MealTypeId")
+                        .HasColumnType("int");
+
+                    b.HasKey("MealId");
+
+                    b.HasIndex("MealTypeId");
+
+                    b.ToTable("Meals");
+                });
+
+            modelBuilder.Entity("EFCore.MealType", b =>
+                {
+                    b.Property<int>("MealTypeId")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("int");
+
+                    SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("MealTypeId"));
+
+                    b.Property<string>("Description")
+                        .HasColumnType("nvarchar(max)");
+
+                    b.HasKey("MealTypeId");
+
+                    b.ToTable("MealTypes");
+                });
+
+            modelBuilder.Entity("EFCore.Meal", b =>
+                {
+                    b.HasOne("EFCore.MealType", "MealType")
+                        .WithMany("Meals")
+                        .HasForeignKey("MealTypeId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
+                    b.Navigation("MealType");
+                });
+
+            modelBuilder.Entity("EFCore.MealType", b =>
+                {
+                    b.Navigation("Meals");
+                });
+#pragma warning restore 612, 618
+        }
+    }
+}
diff --git a/Tasks/Lab9/EntityFramework/EFCore/Program.cs b/Tasks/Lab9/EntityFramework/EFCore/Program.cs
new file mode 100644
index 0000000..7586e61
--- /dev/null
+++ b/Tasks/Lab9/EntityFramework/EFCore/Program.cs
@@ -0,0 +1,12 @@
+namespace EFCore
+{
+    internal class Program
+    {
+        static void Main(string[] args)
+        {
+            var db = new BloggingContext();
+            db.Add(new MealType { Description = "Schockoladenkuchen" });
+            db.SaveChanges();
+        }
+    }
+}
diff --git a/Tasks/Lab9/EntityFramework/EntityFramework.sln b/Tasks/Lab9/EntityFramework/EntityFramework.sln
index 9cbdd68..6b215b9 100644
--- a/Tasks/Lab9/EntityFramework/EntityFramework.sln
+++ b/Tasks/Lab9/EntityFramework/EntityFramework.sln
@@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EF6", "EF6\EF6.csproj", "{7
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EF6_Code_First", "EF6_Code_First\EF6_Code_First.csproj", "{9E452FB4-1B73-4A13-B338-FA33CD0866BC}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EFCore", "EFCore\EFCore.csproj", "{3EBCAB65-AE15-4103-AFCA-AD90DFB9E2DB}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
 		{9E452FB4-1B73-4A13-B338-FA33CD0866BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{9E452FB4-1B73-4A13-B338-FA33CD0866BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{9E452FB4-1B73-4A13-B338-FA33CD0866BC}.Release|Any CPU.Build.0 = Release|Any CPU
+		{3EBCAB65-AE15-4103-AFCA-AD90DFB9E2DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{3EBCAB65-AE15-4103-AFCA-AD90DFB9E2DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{3EBCAB65-AE15-4103-AFCA-AD90DFB9E2DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{3EBCAB65-AE15-4103-AFCA-AD90DFB9E2DB}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE