Create an EF project in .NET Core

This commit is contained in:
Manuel Thalmann 2024-06-06 21:09:24 +02:00
parent 727406aa4a
commit 62385faa76
7 changed files with 304 additions and 0 deletions

View file

@ -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; }
}
}

View file

@ -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>

View file

@ -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
}
}
}

View file

@ -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");
}
}
}

View file

@ -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
}
}
}

View file

@ -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();
}
}
}

View file

@ -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