Fix Shop-Repair relationship

This commit is contained in:
ShabOl 2024-05-05 21:41:04 +04:00
parent 1d24f91773
commit 67bffd6798
6 changed files with 290 additions and 7 deletions

View File

@ -9,7 +9,7 @@ namespace AutoWorkshopDatabaseImplement
{
if (OptionsBuilder.IsConfigured == false)
{
OptionsBuilder.UseNpgsql(@"Host=localhost;Database=AutoWorkshop_Hard;Username=postgres;Password=admin");
OptionsBuilder.UseNpgsql(@"Host=localhost;Port=5000;Database=AutoWorkshop_Hard;Username=postgres;Password=admin");
}
base.OnConfiguring(OptionsBuilder);

View File

@ -0,0 +1,250 @@
// <auto-generated />
using System;
using AutoWorkshopDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace AutoWorkshopDatabaseImplement.Migrations
{
[DbContext(typeof(AutoWorkshopDatabase))]
[Migration("20240505173724_Fix Shop-Repair relationship")]
partial class FixShopRepairrelationship
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.17")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Component", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("text");
b.Property<double>("Cost")
.HasColumnType("double precision");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<DateTime>("DateCreate")
.HasColumnType("timestamp without time zone");
b.Property<DateTime?>("DateImplement")
.HasColumnType("timestamp without time zone");
b.Property<int>("RepairId")
.HasColumnType("integer");
b.Property<int>("Status")
.HasColumnType("integer");
b.Property<double>("Sum")
.HasColumnType("double precision");
b.HasKey("Id");
b.HasIndex("RepairId");
b.ToTable("Orders");
});
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Repair", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<double>("Price")
.HasColumnType("double precision");
b.Property<string>("RepairName")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Repairs");
});
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.RepairComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("ComponentId")
.HasColumnType("integer");
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<int>("RepairId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("RepairId");
b.ToTable("RepairComponents");
});
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Address")
.IsRequired()
.HasColumnType("text");
b.Property<DateTime>("OpeningDate")
.HasColumnType("timestamp without time zone");
b.Property<int>("RepairsMaxCount")
.HasColumnType("integer");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.ShopRepair", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<int>("RepairId")
.HasColumnType("integer");
b.Property<int>("ShopId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("RepairId");
b.HasIndex("ShopId");
b.ToTable("ShopRepairs");
});
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Order", b =>
{
b.HasOne("AutoWorkshopDatabaseImplement.Models.Repair", "Repair")
.WithMany("Orders")
.HasForeignKey("RepairId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Repair");
});
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.RepairComponent", b =>
{
b.HasOne("AutoWorkshopDatabaseImplement.Models.Component", "Component")
.WithMany("RepairComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("AutoWorkshopDatabaseImplement.Models.Repair", "Repair")
.WithMany("Components")
.HasForeignKey("RepairId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Repair");
});
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.ShopRepair", b =>
{
b.HasOne("AutoWorkshopDatabaseImplement.Models.Repair", "Repair")
.WithMany("Shops")
.HasForeignKey("RepairId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("AutoWorkshopDatabaseImplement.Models.Shop", "Shop")
.WithMany("Repairs")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Repair");
b.Navigation("Shop");
});
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Component", b =>
{
b.Navigation("RepairComponents");
});
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Repair", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
b.Navigation("Shops");
});
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Repairs");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace AutoWorkshopDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class FixShopRepairrelationship : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}

View File

@ -207,7 +207,7 @@ namespace AutoWorkshopDatabaseImplement.Migrations
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.ShopRepair", b =>
{
b.HasOne("AutoWorkshopDatabaseImplement.Models.Repair", "Repair")
.WithMany()
.WithMany("Shops")
.HasForeignKey("RepairId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
@ -233,6 +233,8 @@ namespace AutoWorkshopDatabaseImplement.Migrations
b.Navigation("Components");
b.Navigation("Orders");
b.Navigation("Shops");
});
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Shop", b =>

View File

@ -38,7 +38,10 @@ namespace AutoWorkshopDatabaseImplement.Models
[ForeignKey("RepairId")]
public virtual List<Order> Orders { get; set; } = new();
[ForeignKey("RepairId")]
public virtual List<ShopRepair> Shops { get; set; } = new();
public static Repair Create(AutoWorkshopDatabase Context, RepairBindingModel Model)
{
return new Repair()

View File

@ -1,6 +1,7 @@
using AutoWorkshopContracts.BindingModels;
using AutoWorkshopContracts.ViewModels;
using AutoWorkshopDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace AutoWorkshopDatabaseImplement.Models
@ -8,17 +9,25 @@ namespace AutoWorkshopDatabaseImplement.Models
public class Shop : IShopModel
{
public int Id { get; set; }
[Required]
public string ShopName { get; set; } = string.Empty;
[Required]
public string Address { get; set; } = string.Empty;
[Required]
public DateTime OpeningDate { get; set; }
[Required]
public int RepairsMaxCount { get; set; }
[ForeignKey("ShopId")]
public List<ShopRepair> Repairs { get; set; } = new();
private Dictionary<int, (IRepairModel, int)>? _shopRepairs = null;
[NotMapped]
public Dictionary<int, (IRepairModel, int)> ShopRepairs
{
get
@ -36,9 +45,6 @@ namespace AutoWorkshopDatabaseImplement.Models
}
}
[ForeignKey("ShopId")]
public List<ShopRepair> Repairs { get; set; } = new();
public static Shop Create(AutoWorkshopDatabase Context, ShopBindingModel Model)
{
return new Shop()