From c06f4645a970c694e4a957440555744240308cb3 Mon Sep 17 00:00:00 2001 From: marcu Date: Fri, 23 Feb 2024 23:39:45 +0100 Subject: [PATCH] Add project files. --- Weifer.Database.EF.sln | 25 ++++ Weifer.Database.EF/DatabaseContext.cs | 50 +++++++ Weifer.Database.EF/Entitys/Customer.cs | 19 +++ Weifer.Database.EF/Entitys/ShoppingItem.cs | 17 +++ Weifer.Database.EF/Entitys/ShoppingList.cs | 16 +++ .../20240223223336_init.Designer.cs | 134 ++++++++++++++++++ .../Migrations/20240223223336_init.cs | 94 ++++++++++++ .../DatabaseContextModelSnapshot.cs | 131 +++++++++++++++++ Weifer.Database.EF/Weifer.Database.EF.csproj | 25 ++++ Weifer.Database.EF/appsettings.json | 5 + 10 files changed, 516 insertions(+) create mode 100644 Weifer.Database.EF.sln create mode 100644 Weifer.Database.EF/DatabaseContext.cs create mode 100644 Weifer.Database.EF/Entitys/Customer.cs create mode 100644 Weifer.Database.EF/Entitys/ShoppingItem.cs create mode 100644 Weifer.Database.EF/Entitys/ShoppingList.cs create mode 100644 Weifer.Database.EF/Migrations/20240223223336_init.Designer.cs create mode 100644 Weifer.Database.EF/Migrations/20240223223336_init.cs create mode 100644 Weifer.Database.EF/Migrations/DatabaseContextModelSnapshot.cs create mode 100644 Weifer.Database.EF/Weifer.Database.EF.csproj create mode 100644 Weifer.Database.EF/appsettings.json diff --git a/Weifer.Database.EF.sln b/Weifer.Database.EF.sln new file mode 100644 index 0000000..a8652f5 --- /dev/null +++ b/Weifer.Database.EF.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34526.213 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Weifer.Database.EF", "Weifer.Database.EF\Weifer.Database.EF.csproj", "{20B939C6-8D62-4DCA-B47D-AE17FE96A0CA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {20B939C6-8D62-4DCA-B47D-AE17FE96A0CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {20B939C6-8D62-4DCA-B47D-AE17FE96A0CA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {20B939C6-8D62-4DCA-B47D-AE17FE96A0CA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {20B939C6-8D62-4DCA-B47D-AE17FE96A0CA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {493039A3-BE7C-4200-8165-C1B32A1461F9} + EndGlobalSection +EndGlobal diff --git a/Weifer.Database.EF/DatabaseContext.cs b/Weifer.Database.EF/DatabaseContext.cs new file mode 100644 index 0000000..59a103b --- /dev/null +++ b/Weifer.Database.EF/DatabaseContext.cs @@ -0,0 +1,50 @@ + +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using System.Collections.Generic; +using System.Net; +using Weifer.Database.EF.Entitys; + +namespace Weifer.Database.EF +{ + public class DatabaseContext : DbContext + { + public DatabaseContext() { } + public DatabaseContext(DbContextOptions options) : base(options) { } + public virtual DbSet Customers { get; set; } + public virtual DbSet ShoppingLists { get; set; } + public virtual DbSet ShoppingItems { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (!optionsBuilder.IsConfigured) + { + var configuration = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) + .Build(); + + var connectionString = configuration.GetConnectionString("ShoppingApp"); + optionsBuilder.UseSqlServer(connectionString); + } + } + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + + modelBuilder.Entity() + .HasMany(c => c.ShoppingLists) + .WithOne() + .HasForeignKey(sl => sl.CustomerId) + .OnDelete(DeleteBehavior.Cascade); + + + modelBuilder.Entity() + .HasMany(sl => sl.ShoppingItems) + .WithOne() + .HasForeignKey(si => si.ShoppingListId) + .OnDelete(DeleteBehavior.Cascade); + } + } +} diff --git a/Weifer.Database.EF/Entitys/Customer.cs b/Weifer.Database.EF/Entitys/Customer.cs new file mode 100644 index 0000000..e36418c --- /dev/null +++ b/Weifer.Database.EF/Entitys/Customer.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Weifer.Database.EF.Entitys +{ + public class Customer + { + public Guid CustomerId { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public string Email { get; set; } + public string PasswordHash { get; set; } + public DateTime CreatedOn { get; set; } + public ICollection ShoppingLists { get; set; } + } +} diff --git a/Weifer.Database.EF/Entitys/ShoppingItem.cs b/Weifer.Database.EF/Entitys/ShoppingItem.cs new file mode 100644 index 0000000..62bf451 --- /dev/null +++ b/Weifer.Database.EF/Entitys/ShoppingItem.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Weifer.Database.EF.Entitys +{ + public class ShoppingItem + { + public Guid ShoppingItemId { get; set; } + public Guid ShoppingListId { get; set; } + public string Description { get; set; } + public int Number { get; set; } = 0; + public bool Purchased { get; set; } = false; + } +} diff --git a/Weifer.Database.EF/Entitys/ShoppingList.cs b/Weifer.Database.EF/Entitys/ShoppingList.cs new file mode 100644 index 0000000..f7db2f8 --- /dev/null +++ b/Weifer.Database.EF/Entitys/ShoppingList.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Weifer.Database.EF.Entitys +{ + public class ShoppingList + { + public Guid ShoppingListId { get; set; } + public Guid CustomerId { get; set; } + public string ShoppingListName { get; set; } + public ICollection ShoppingItems { get; set; } + } +} diff --git a/Weifer.Database.EF/Migrations/20240223223336_init.Designer.cs b/Weifer.Database.EF/Migrations/20240223223336_init.Designer.cs new file mode 100644 index 0000000..7514e8d --- /dev/null +++ b/Weifer.Database.EF/Migrations/20240223223336_init.Designer.cs @@ -0,0 +1,134 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Weifer.Database.EF; + +#nullable disable + +namespace Weifer.Database.EF.Migrations +{ + [DbContext(typeof(DatabaseContext))] + [Migration("20240223223336_init")] + partial class init + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.2") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("Weifer.Database.EF.Entitys.Customer", b => + { + b.Property("CustomerId") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreatedOn") + .HasColumnType("datetime2"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PasswordHash") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("CustomerId"); + + b.ToTable("Customers"); + }); + + modelBuilder.Entity("Weifer.Database.EF.Entitys.ShoppingItem", b => + { + b.Property("ShoppingItemId") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("Description") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Number") + .HasColumnType("int"); + + b.Property("Purchased") + .HasColumnType("bit"); + + b.Property("ShoppingListId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("ShoppingItemId"); + + b.HasIndex("ShoppingListId"); + + b.ToTable("ShoppingItems"); + }); + + modelBuilder.Entity("Weifer.Database.EF.Entitys.ShoppingList", b => + { + b.Property("ShoppingListId") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CustomerId") + .HasColumnType("uniqueidentifier"); + + b.Property("ShoppingListName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("ShoppingListId"); + + b.HasIndex("CustomerId"); + + b.ToTable("ShoppingLists"); + }); + + modelBuilder.Entity("Weifer.Database.EF.Entitys.ShoppingItem", b => + { + b.HasOne("Weifer.Database.EF.Entitys.ShoppingList", null) + .WithMany("ShoppingItems") + .HasForeignKey("ShoppingListId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Weifer.Database.EF.Entitys.ShoppingList", b => + { + b.HasOne("Weifer.Database.EF.Entitys.Customer", null) + .WithMany("ShoppingLists") + .HasForeignKey("CustomerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Weifer.Database.EF.Entitys.Customer", b => + { + b.Navigation("ShoppingLists"); + }); + + modelBuilder.Entity("Weifer.Database.EF.Entitys.ShoppingList", b => + { + b.Navigation("ShoppingItems"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Weifer.Database.EF/Migrations/20240223223336_init.cs b/Weifer.Database.EF/Migrations/20240223223336_init.cs new file mode 100644 index 0000000..633228f --- /dev/null +++ b/Weifer.Database.EF/Migrations/20240223223336_init.cs @@ -0,0 +1,94 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Weifer.Database.EF.Migrations +{ + /// + public partial class init : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Customers", + columns: table => new + { + CustomerId = table.Column(type: "uniqueidentifier", nullable: false), + FirstName = table.Column(type: "nvarchar(max)", nullable: false), + LastName = table.Column(type: "nvarchar(max)", nullable: false), + Email = table.Column(type: "nvarchar(max)", nullable: false), + PasswordHash = table.Column(type: "nvarchar(max)", nullable: false), + CreatedOn = table.Column(type: "datetime2", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Customers", x => x.CustomerId); + }); + + migrationBuilder.CreateTable( + name: "ShoppingLists", + columns: table => new + { + ShoppingListId = table.Column(type: "uniqueidentifier", nullable: false), + CustomerId = table.Column(type: "uniqueidentifier", nullable: false), + ShoppingListName = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShoppingLists", x => x.ShoppingListId); + table.ForeignKey( + name: "FK_ShoppingLists_Customers_CustomerId", + column: x => x.CustomerId, + principalTable: "Customers", + principalColumn: "CustomerId", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "ShoppingItems", + columns: table => new + { + ShoppingItemId = table.Column(type: "uniqueidentifier", nullable: false), + ShoppingListId = table.Column(type: "uniqueidentifier", nullable: false), + Description = table.Column(type: "nvarchar(max)", nullable: false), + Number = table.Column(type: "int", nullable: false), + Purchased = table.Column(type: "bit", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShoppingItems", x => x.ShoppingItemId); + table.ForeignKey( + name: "FK_ShoppingItems_ShoppingLists_ShoppingListId", + column: x => x.ShoppingListId, + principalTable: "ShoppingLists", + principalColumn: "ShoppingListId", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ShoppingItems_ShoppingListId", + table: "ShoppingItems", + column: "ShoppingListId"); + + migrationBuilder.CreateIndex( + name: "IX_ShoppingLists_CustomerId", + table: "ShoppingLists", + column: "CustomerId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ShoppingItems"); + + migrationBuilder.DropTable( + name: "ShoppingLists"); + + migrationBuilder.DropTable( + name: "Customers"); + } + } +} diff --git a/Weifer.Database.EF/Migrations/DatabaseContextModelSnapshot.cs b/Weifer.Database.EF/Migrations/DatabaseContextModelSnapshot.cs new file mode 100644 index 0000000..560d3f4 --- /dev/null +++ b/Weifer.Database.EF/Migrations/DatabaseContextModelSnapshot.cs @@ -0,0 +1,131 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Weifer.Database.EF; + +#nullable disable + +namespace Weifer.Database.EF.Migrations +{ + [DbContext(typeof(DatabaseContext))] + partial class DatabaseContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.2") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("Weifer.Database.EF.Entitys.Customer", b => + { + b.Property("CustomerId") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreatedOn") + .HasColumnType("datetime2"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PasswordHash") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("CustomerId"); + + b.ToTable("Customers"); + }); + + modelBuilder.Entity("Weifer.Database.EF.Entitys.ShoppingItem", b => + { + b.Property("ShoppingItemId") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("Description") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Number") + .HasColumnType("int"); + + b.Property("Purchased") + .HasColumnType("bit"); + + b.Property("ShoppingListId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("ShoppingItemId"); + + b.HasIndex("ShoppingListId"); + + b.ToTable("ShoppingItems"); + }); + + modelBuilder.Entity("Weifer.Database.EF.Entitys.ShoppingList", b => + { + b.Property("ShoppingListId") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CustomerId") + .HasColumnType("uniqueidentifier"); + + b.Property("ShoppingListName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("ShoppingListId"); + + b.HasIndex("CustomerId"); + + b.ToTable("ShoppingLists"); + }); + + modelBuilder.Entity("Weifer.Database.EF.Entitys.ShoppingItem", b => + { + b.HasOne("Weifer.Database.EF.Entitys.ShoppingList", null) + .WithMany("ShoppingItems") + .HasForeignKey("ShoppingListId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Weifer.Database.EF.Entitys.ShoppingList", b => + { + b.HasOne("Weifer.Database.EF.Entitys.Customer", null) + .WithMany("ShoppingLists") + .HasForeignKey("CustomerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Weifer.Database.EF.Entitys.Customer", b => + { + b.Navigation("ShoppingLists"); + }); + + modelBuilder.Entity("Weifer.Database.EF.Entitys.ShoppingList", b => + { + b.Navigation("ShoppingItems"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Weifer.Database.EF/Weifer.Database.EF.csproj b/Weifer.Database.EF/Weifer.Database.EF.csproj new file mode 100644 index 0000000..605c691 --- /dev/null +++ b/Weifer.Database.EF/Weifer.Database.EF.csproj @@ -0,0 +1,25 @@ + + + + net8.0 + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + diff --git a/Weifer.Database.EF/appsettings.json b/Weifer.Database.EF/appsettings.json new file mode 100644 index 0000000..0378d92 --- /dev/null +++ b/Weifer.Database.EF/appsettings.json @@ -0,0 +1,5 @@ +{ + "ConnectionStrings": { + "ShoppingApp": "Server=192.168.0.187,1433;Database=ShoppingApp;User Id=sa;Password=Su746MoWyM8CtqgiCc3oWzsb;Trusted_Connection=False;TrustServerCertificate=True" + } +} \ No newline at end of file