From 62385faa76def4caed9f69d07ddb0821c8c8dfa5 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann 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 MealTypes { get; set; } + public DbSet 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 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 @@ + + + + Exe + net8.0 + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + 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 @@ +// +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 + { + /// + 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("MealId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("MealId")); + + b.Property("MealDescription") + .HasColumnType("nvarchar(max)"); + + b.Property("MealName") + .HasColumnType("nvarchar(max)"); + + b.Property("MealTypeId") + .HasColumnType("int"); + + b.HasKey("MealId"); + + b.HasIndex("MealTypeId"); + + b.ToTable("Meals"); + }); + + modelBuilder.Entity("EFCore.MealType", b => + { + b.Property("MealTypeId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("MealTypeId")); + + b.Property("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 +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "MealTypes", + columns: table => new + { + MealTypeId = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Description = table.Column(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_MealTypes", x => x.MealTypeId); + }); + + migrationBuilder.CreateTable( + name: "Meals", + columns: table => new + { + MealId = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + MealName = table.Column(type: "nvarchar(max)", nullable: true), + MealDescription = table.Column(type: "nvarchar(max)", nullable: true), + MealTypeId = table.Column(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"); + } + + /// + 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 @@ +// +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("MealId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("MealId")); + + b.Property("MealDescription") + .HasColumnType("nvarchar(max)"); + + b.Property("MealName") + .HasColumnType("nvarchar(max)"); + + b.Property("MealTypeId") + .HasColumnType("int"); + + b.HasKey("MealId"); + + b.HasIndex("MealTypeId"); + + b.ToTable("Meals"); + }); + + modelBuilder.Entity("EFCore.MealType", b => + { + b.Property("MealTypeId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("MealTypeId")); + + b.Property("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