From cc07596c65bc529e682591b8350c6018b355ed58 Mon Sep 17 00:00:00 2001 From: BabychMaksym Date: Mon, 18 Mar 2024 05:56:12 +0200 Subject: [PATCH 1/5] add HomeWork 21 --- HomeWork21/HomeWork21.sln | 25 +++ HomeWork21/HomeWork21/App.cs | 25 +++ .../HomeWork21/Data/AplicationDbContext.cs | 27 +++ .../Data/AplicationDbContextFactory.cs | 29 +++ .../HomeWork21/Data/Entities/BreedEntity.cs | 12 ++ .../Data/Entities/CategoryEntity.cs | 14 ++ .../Data/Entities/LocationEntity.cs | 11 ++ .../HomeWork21/Data/Entities/PetEntity.cs | 15 ++ .../EntitiesConfigure/BreedEntityConfigure.cs | 22 +++ .../CategoryEntityConfigure.cs | 29 +++ .../LocationEntityConfigure.cs | 27 +++ .../PetEntityConfiguration.cs | 18 ++ HomeWork21/HomeWork21/HomeWork21.csproj | 35 ++++ .../20240318034724_InitialCreate.Designer.cs | 186 ++++++++++++++++++ .../20240318034724_InitialCreate.cs | 134 +++++++++++++ .../AplicationDbContextModelSnapshot.cs | 183 +++++++++++++++++ HomeWork21/HomeWork21/Models/Breed.cs | 8 + HomeWork21/HomeWork21/Models/Category.cs | 8 + HomeWork21/HomeWork21/Models/Location.cs | 8 + HomeWork21/HomeWork21/Models/Pet.cs | 15 ++ HomeWork21/HomeWork21/Program.cs | 46 +++++ HomeWork21/HomeWork21/config.json | 8 + 22 files changed, 885 insertions(+) create mode 100644 HomeWork21/HomeWork21.sln create mode 100644 HomeWork21/HomeWork21/App.cs create mode 100644 HomeWork21/HomeWork21/Data/AplicationDbContext.cs create mode 100644 HomeWork21/HomeWork21/Data/AplicationDbContextFactory.cs create mode 100644 HomeWork21/HomeWork21/Data/Entities/BreedEntity.cs create mode 100644 HomeWork21/HomeWork21/Data/Entities/CategoryEntity.cs create mode 100644 HomeWork21/HomeWork21/Data/Entities/LocationEntity.cs create mode 100644 HomeWork21/HomeWork21/Data/Entities/PetEntity.cs create mode 100644 HomeWork21/HomeWork21/Data/EntitiesConfigure/BreedEntityConfigure.cs create mode 100644 HomeWork21/HomeWork21/Data/EntitiesConfigure/CategoryEntityConfigure.cs create mode 100644 HomeWork21/HomeWork21/Data/EntitiesConfigure/LocationEntityConfigure.cs create mode 100644 HomeWork21/HomeWork21/Data/EntitiesConfigure/PetEntityConfiguration.cs create mode 100644 HomeWork21/HomeWork21/HomeWork21.csproj create mode 100644 HomeWork21/HomeWork21/Migrations/20240318034724_InitialCreate.Designer.cs create mode 100644 HomeWork21/HomeWork21/Migrations/20240318034724_InitialCreate.cs create mode 100644 HomeWork21/HomeWork21/Migrations/AplicationDbContextModelSnapshot.cs create mode 100644 HomeWork21/HomeWork21/Models/Breed.cs create mode 100644 HomeWork21/HomeWork21/Models/Category.cs create mode 100644 HomeWork21/HomeWork21/Models/Location.cs create mode 100644 HomeWork21/HomeWork21/Models/Pet.cs create mode 100644 HomeWork21/HomeWork21/Program.cs create mode 100644 HomeWork21/HomeWork21/config.json diff --git a/HomeWork21/HomeWork21.sln b/HomeWork21/HomeWork21.sln new file mode 100644 index 0000000..7757f63 --- /dev/null +++ b/HomeWork21/HomeWork21.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34525.116 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HomeWork21", "HomeWork21\HomeWork21.csproj", "{0BEE645B-6D86-404B-9793-B72321132D9A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0BEE645B-6D86-404B-9793-B72321132D9A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0BEE645B-6D86-404B-9793-B72321132D9A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0BEE645B-6D86-404B-9793-B72321132D9A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0BEE645B-6D86-404B-9793-B72321132D9A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0B529666-E964-4A14-87C7-F3ED5FABA8B5} + EndGlobalSection +EndGlobal diff --git a/HomeWork21/HomeWork21/App.cs b/HomeWork21/HomeWork21/App.cs new file mode 100644 index 0000000..7e2b8fa --- /dev/null +++ b/HomeWork21/HomeWork21/App.cs @@ -0,0 +1,25 @@ +using HomeWork21.Data; +using Microsoft.EntityFrameworkCore; + +namespace HomeWork21 +{ + internal class App + { + private readonly AplicationDbContext _dbContext; + + public App(AplicationDbContext dbContext) + { + _dbContext = dbContext; + } + + public async Task StartUpAsync() + { + var data = await _dbContext.Pets.ToListAsync(); + foreach (var location in data) + { + Console.WriteLine(location.Id + " " + location.Name); + } + + } + } +} diff --git a/HomeWork21/HomeWork21/Data/AplicationDbContext.cs b/HomeWork21/HomeWork21/Data/AplicationDbContext.cs new file mode 100644 index 0000000..943447d --- /dev/null +++ b/HomeWork21/HomeWork21/Data/AplicationDbContext.cs @@ -0,0 +1,27 @@ +using HomeWork21.Data.Entities; +using HomeWork21.Data.EntitiesConfigure; +using Microsoft.EntityFrameworkCore; + +namespace HomeWork21.Data +{ + internal class AplicationDbContext : DbContext + { + public AplicationDbContext(DbContextOptions options) + : base(options) + { } + + public DbSet Breeds { get; set; } = null!; + public DbSet Categories { get; set; } = null!; + public DbSet Locations { get; set; } = null!; + public DbSet Pets { get; set; } = null!; + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.ApplyConfiguration(new CategoryEntityConfigure()); + modelBuilder.ApplyConfiguration(new BreedEntityConfigure()); + modelBuilder.ApplyConfiguration(new LocationEntityConfigure()); + modelBuilder.ApplyConfiguration(new PetEntityConfiguration()); + modelBuilder.UseHiLo(); + } + } +} diff --git a/HomeWork21/HomeWork21/Data/AplicationDbContextFactory.cs b/HomeWork21/HomeWork21/Data/AplicationDbContextFactory.cs new file mode 100644 index 0000000..d4b2350 --- /dev/null +++ b/HomeWork21/HomeWork21/Data/AplicationDbContextFactory.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Design; +using Microsoft.Extensions.Configuration; + +namespace HomeWork21.Data +{ + internal class AplicationDbContextFactory : IDesignTimeDbContextFactory + { + public AplicationDbContext CreateDbContext(string[] args) + { + var optionsBuilder = new DbContextOptionsBuilder(); + + var builder = new ConfigurationBuilder(); + builder.SetBasePath(Directory.GetCurrentDirectory()); + builder.AddJsonFile("config.json"); + var config = builder.Build(); + + var connectionString = config.GetConnectionString("DefaultConnection"); + + optionsBuilder + .UseSqlServer(connectionString, + option => option + .CommandTimeout((int)TimeSpan + .FromMinutes(10).TotalSeconds)); + + return new AplicationDbContext(optionsBuilder.Options); + } + } +} \ No newline at end of file diff --git a/HomeWork21/HomeWork21/Data/Entities/BreedEntity.cs b/HomeWork21/HomeWork21/Data/Entities/BreedEntity.cs new file mode 100644 index 0000000..a0a49fd --- /dev/null +++ b/HomeWork21/HomeWork21/Data/Entities/BreedEntity.cs @@ -0,0 +1,12 @@ +namespace HomeWork21.Data.Entities +{ + internal class BreedEntity + { + public int Id { get; set; } + public string BreedName { get; set; } = null!; + public int PetId { get; set; } + public PetEntity Pet { get; set; } = null!; + + public List Category { get; set; } = new List(); + } +} diff --git a/HomeWork21/HomeWork21/Data/Entities/CategoryEntity.cs b/HomeWork21/HomeWork21/Data/Entities/CategoryEntity.cs new file mode 100644 index 0000000..e0c1885 --- /dev/null +++ b/HomeWork21/HomeWork21/Data/Entities/CategoryEntity.cs @@ -0,0 +1,14 @@ +namespace HomeWork21.Data.Entities +{ + internal class CategoryEntity + { + public int Id { get; set; } + public string CategoryName { get; set; } = null!; + + public int PetId { get; set; } + public PetEntity? Pet { get; set; } + + public int BreedId { get; set; } + public BreedEntity? Breed { get; set; } + } +} diff --git a/HomeWork21/HomeWork21/Data/Entities/LocationEntity.cs b/HomeWork21/HomeWork21/Data/Entities/LocationEntity.cs new file mode 100644 index 0000000..9843ce0 --- /dev/null +++ b/HomeWork21/HomeWork21/Data/Entities/LocationEntity.cs @@ -0,0 +1,11 @@ +namespace HomeWork21.Data.Entities +{ + internal class LocationEntity + { + public int Id { get; set; } + public string? LocationName { get; set; } + + public int PetId { get; set; } + public PetEntity? Pet { get; set; } + } +} diff --git a/HomeWork21/HomeWork21/Data/Entities/PetEntity.cs b/HomeWork21/HomeWork21/Data/Entities/PetEntity.cs new file mode 100644 index 0000000..e4b5ae5 --- /dev/null +++ b/HomeWork21/HomeWork21/Data/Entities/PetEntity.cs @@ -0,0 +1,15 @@ +namespace HomeWork21.Data.Entities +{ + internal class PetEntity + { + public int Id { get; set; } + public string Name { get; set; } = null!; + public float Age { get; set; } + public string? ImageUrl { get; set; } + public string? Description { get; set; } + + public List Category { get; set; } = new List(); + public List Breed { get; set; } = new List(); + public List LocationName { get; set; } = new List(); + } +} diff --git a/HomeWork21/HomeWork21/Data/EntitiesConfigure/BreedEntityConfigure.cs b/HomeWork21/HomeWork21/Data/EntitiesConfigure/BreedEntityConfigure.cs new file mode 100644 index 0000000..f1ffc20 --- /dev/null +++ b/HomeWork21/HomeWork21/Data/EntitiesConfigure/BreedEntityConfigure.cs @@ -0,0 +1,22 @@ +using HomeWork21.Data.Entities; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace HomeWork21.Data.EntitiesConfigure +{ + internal class BreedEntityConfigure : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + builder.Property(p => p.BreedName).IsRequired().HasColumnName("breed_name"); + builder.Property(p => p.PetId).IsRequired(); + + builder + .HasOne(o=>o.Pet) + .WithMany(m => m.Breed) + .HasForeignKey(o => o.PetId) + .OnDelete(DeleteBehavior.Cascade); + } + } +} diff --git a/HomeWork21/HomeWork21/Data/EntitiesConfigure/CategoryEntityConfigure.cs b/HomeWork21/HomeWork21/Data/EntitiesConfigure/CategoryEntityConfigure.cs new file mode 100644 index 0000000..0ec174b --- /dev/null +++ b/HomeWork21/HomeWork21/Data/EntitiesConfigure/CategoryEntityConfigure.cs @@ -0,0 +1,29 @@ +using HomeWork21.Data.Entities; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace HomeWork21.Data.EntitiesConfigure +{ + internal class CategoryEntityConfigure : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(k => k.Id); + builder.Property(p => p.CategoryName).IsRequired().HasColumnName("category_name"); + builder.Property(p => p.PetId).IsRequired(); + builder.Property(p => p.BreedId).IsRequired(); + + builder + .HasOne(o=>o.Pet) + .WithMany(m=>m.Category) + .HasForeignKey(o => o.PetId) + .OnDelete(DeleteBehavior.ClientSetNull); + + builder + .HasOne(o => o.Breed) + .WithMany(m => m.Category) + .HasForeignKey(k => k.BreedId) + .OnDelete(DeleteBehavior.ClientSetNull); + } + } +} diff --git a/HomeWork21/HomeWork21/Data/EntitiesConfigure/LocationEntityConfigure.cs b/HomeWork21/HomeWork21/Data/EntitiesConfigure/LocationEntityConfigure.cs new file mode 100644 index 0000000..aca37ef --- /dev/null +++ b/HomeWork21/HomeWork21/Data/EntitiesConfigure/LocationEntityConfigure.cs @@ -0,0 +1,27 @@ +using HomeWork21.Data.Entities; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace HomeWork21.Data.EntitiesConfigure +{ + internal class LocationEntityConfigure : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(k => k.Id); + builder.Property(p=>p.PetId).IsRequired(); + + builder + .Property(p => p.LocationName) + .IsRequired() + .HasColumnName("location_name"); + + + builder + .HasOne(o => o.Pet) + .WithMany(o => o.LocationName) + .HasForeignKey(k => k.PetId) + .OnDelete(DeleteBehavior.Cascade); + } + } +} diff --git a/HomeWork21/HomeWork21/Data/EntitiesConfigure/PetEntityConfiguration.cs b/HomeWork21/HomeWork21/Data/EntitiesConfigure/PetEntityConfiguration.cs new file mode 100644 index 0000000..3605303 --- /dev/null +++ b/HomeWork21/HomeWork21/Data/EntitiesConfigure/PetEntityConfiguration.cs @@ -0,0 +1,18 @@ +using HomeWork21.Data.Entities; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace HomeWork21.Data.EntitiesConfigure +{ + internal class PetEntityConfiguration : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(k => k.Id); + builder.Property(p=>p.Name).IsRequired(); + builder.Property(p => p.Age).IsRequired(); + builder.Property(p => p.ImageUrl).HasColumnName("image_url"); + builder.Property(p => p.Description); + } + } +} diff --git a/HomeWork21/HomeWork21/HomeWork21.csproj b/HomeWork21/HomeWork21/HomeWork21.csproj new file mode 100644 index 0000000..6c7480b --- /dev/null +++ b/HomeWork21/HomeWork21/HomeWork21.csproj @@ -0,0 +1,35 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + diff --git a/HomeWork21/HomeWork21/Migrations/20240318034724_InitialCreate.Designer.cs b/HomeWork21/HomeWork21/Migrations/20240318034724_InitialCreate.Designer.cs new file mode 100644 index 0000000..726e228 --- /dev/null +++ b/HomeWork21/HomeWork21/Migrations/20240318034724_InitialCreate.Designer.cs @@ -0,0 +1,186 @@ +// +using HomeWork21.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace HomeWork21.Migrations +{ + [DbContext(typeof(AplicationDbContext))] + [Migration("20240318034724_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseHiLo(modelBuilder, "EntityFrameworkHiLoSequence"); + + modelBuilder.HasSequence("EntityFrameworkHiLoSequence") + .IncrementsBy(10); + + modelBuilder.Entity("HomeWork21.Data.Entities.BreedEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("BreedName") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("breed_name"); + + b.Property("PetId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PetId"); + + b.ToTable("Breeds"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.CategoryEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("BreedId") + .HasColumnType("int"); + + b.Property("CategoryName") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("category_name"); + + b.Property("PetId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("BreedId"); + + b.HasIndex("PetId"); + + b.ToTable("Categories"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.LocationEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("LocationName") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("location_name"); + + b.Property("PetId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PetId"); + + b.ToTable("Locations"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.PetEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("Age") + .HasColumnType("real"); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("ImageUrl") + .HasColumnType("nvarchar(max)") + .HasColumnName("image_url"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Pets"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.BreedEntity", b => + { + b.HasOne("HomeWork21.Data.Entities.PetEntity", "Pet") + .WithMany("Breed") + .HasForeignKey("PetId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pet"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.CategoryEntity", b => + { + b.HasOne("HomeWork21.Data.Entities.BreedEntity", "Breed") + .WithMany("Category") + .HasForeignKey("BreedId") + .IsRequired(); + + b.HasOne("HomeWork21.Data.Entities.PetEntity", "Pet") + .WithMany("Category") + .HasForeignKey("PetId") + .IsRequired(); + + b.Navigation("Breed"); + + b.Navigation("Pet"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.LocationEntity", b => + { + b.HasOne("HomeWork21.Data.Entities.PetEntity", "Pet") + .WithMany("LocationName") + .HasForeignKey("PetId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pet"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.BreedEntity", b => + { + b.Navigation("Category"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.PetEntity", b => + { + b.Navigation("Breed"); + + b.Navigation("Category"); + + b.Navigation("LocationName"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/HomeWork21/HomeWork21/Migrations/20240318034724_InitialCreate.cs b/HomeWork21/HomeWork21/Migrations/20240318034724_InitialCreate.cs new file mode 100644 index 0000000..25fd643 --- /dev/null +++ b/HomeWork21/HomeWork21/Migrations/20240318034724_InitialCreate.cs @@ -0,0 +1,134 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace HomeWork21.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateSequence( + name: "EntityFrameworkHiLoSequence", + incrementBy: 10); + + migrationBuilder.CreateTable( + name: "Pets", + columns: table => new + { + Id = table.Column(type: "int", nullable: false), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Age = table.Column(type: "real", nullable: false), + image_url = table.Column(type: "nvarchar(max)", nullable: true), + Description = table.Column(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Pets", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Breeds", + columns: table => new + { + Id = table.Column(type: "int", nullable: false), + breed_name = table.Column(type: "nvarchar(max)", nullable: false), + PetId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Breeds", x => x.Id); + table.ForeignKey( + name: "FK_Breeds_Pets_PetId", + column: x => x.PetId, + principalTable: "Pets", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Locations", + columns: table => new + { + Id = table.Column(type: "int", nullable: false), + location_name = table.Column(type: "nvarchar(max)", nullable: false), + PetId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Locations", x => x.Id); + table.ForeignKey( + name: "FK_Locations_Pets_PetId", + column: x => x.PetId, + principalTable: "Pets", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Categories", + columns: table => new + { + Id = table.Column(type: "int", nullable: false), + category_name = table.Column(type: "nvarchar(max)", nullable: false), + PetId = table.Column(type: "int", nullable: false), + BreedId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Categories", x => x.Id); + table.ForeignKey( + name: "FK_Categories_Breeds_BreedId", + column: x => x.BreedId, + principalTable: "Breeds", + principalColumn: "Id"); + table.ForeignKey( + name: "FK_Categories_Pets_PetId", + column: x => x.PetId, + principalTable: "Pets", + principalColumn: "Id"); + }); + + migrationBuilder.CreateIndex( + name: "IX_Breeds_PetId", + table: "Breeds", + column: "PetId"); + + migrationBuilder.CreateIndex( + name: "IX_Categories_BreedId", + table: "Categories", + column: "BreedId"); + + migrationBuilder.CreateIndex( + name: "IX_Categories_PetId", + table: "Categories", + column: "PetId"); + + migrationBuilder.CreateIndex( + name: "IX_Locations_PetId", + table: "Locations", + column: "PetId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Categories"); + + migrationBuilder.DropTable( + name: "Locations"); + + migrationBuilder.DropTable( + name: "Breeds"); + + migrationBuilder.DropTable( + name: "Pets"); + + migrationBuilder.DropSequence( + name: "EntityFrameworkHiLoSequence"); + } + } +} diff --git a/HomeWork21/HomeWork21/Migrations/AplicationDbContextModelSnapshot.cs b/HomeWork21/HomeWork21/Migrations/AplicationDbContextModelSnapshot.cs new file mode 100644 index 0000000..973c6c7 --- /dev/null +++ b/HomeWork21/HomeWork21/Migrations/AplicationDbContextModelSnapshot.cs @@ -0,0 +1,183 @@ +// +using HomeWork21.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace HomeWork21.Migrations +{ + [DbContext(typeof(AplicationDbContext))] + partial class AplicationDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseHiLo(modelBuilder, "EntityFrameworkHiLoSequence"); + + modelBuilder.HasSequence("EntityFrameworkHiLoSequence") + .IncrementsBy(10); + + modelBuilder.Entity("HomeWork21.Data.Entities.BreedEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("BreedName") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("breed_name"); + + b.Property("PetId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PetId"); + + b.ToTable("Breeds"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.CategoryEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("BreedId") + .HasColumnType("int"); + + b.Property("CategoryName") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("category_name"); + + b.Property("PetId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("BreedId"); + + b.HasIndex("PetId"); + + b.ToTable("Categories"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.LocationEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("LocationName") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("location_name"); + + b.Property("PetId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PetId"); + + b.ToTable("Locations"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.PetEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("Age") + .HasColumnType("real"); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("ImageUrl") + .HasColumnType("nvarchar(max)") + .HasColumnName("image_url"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Pets"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.BreedEntity", b => + { + b.HasOne("HomeWork21.Data.Entities.PetEntity", "Pet") + .WithMany("Breed") + .HasForeignKey("PetId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pet"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.CategoryEntity", b => + { + b.HasOne("HomeWork21.Data.Entities.BreedEntity", "Breed") + .WithMany("Category") + .HasForeignKey("BreedId") + .IsRequired(); + + b.HasOne("HomeWork21.Data.Entities.PetEntity", "Pet") + .WithMany("Category") + .HasForeignKey("PetId") + .IsRequired(); + + b.Navigation("Breed"); + + b.Navigation("Pet"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.LocationEntity", b => + { + b.HasOne("HomeWork21.Data.Entities.PetEntity", "Pet") + .WithMany("LocationName") + .HasForeignKey("PetId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pet"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.BreedEntity", b => + { + b.Navigation("Category"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.PetEntity", b => + { + b.Navigation("Breed"); + + b.Navigation("Category"); + + b.Navigation("LocationName"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/HomeWork21/HomeWork21/Models/Breed.cs b/HomeWork21/HomeWork21/Models/Breed.cs new file mode 100644 index 0000000..131854f --- /dev/null +++ b/HomeWork21/HomeWork21/Models/Breed.cs @@ -0,0 +1,8 @@ +namespace HomeWork21.Models +{ + internal class Breed + { + public int Id { get; set; } + public string Name { get; set; } = null!; + } +} diff --git a/HomeWork21/HomeWork21/Models/Category.cs b/HomeWork21/HomeWork21/Models/Category.cs new file mode 100644 index 0000000..51eff11 --- /dev/null +++ b/HomeWork21/HomeWork21/Models/Category.cs @@ -0,0 +1,8 @@ +namespace HomeWork21.Models +{ + internal class Category + { + public int Id { get; set; } + public string Name { get; set; } = null; + } +} diff --git a/HomeWork21/HomeWork21/Models/Location.cs b/HomeWork21/HomeWork21/Models/Location.cs new file mode 100644 index 0000000..a6a444d --- /dev/null +++ b/HomeWork21/HomeWork21/Models/Location.cs @@ -0,0 +1,8 @@ +namespace HomeWork21.Models +{ + internal class Location + { + public int Id { get; set; } + public string Name { get; set; } = null!; + } +} diff --git a/HomeWork21/HomeWork21/Models/Pet.cs b/HomeWork21/HomeWork21/Models/Pet.cs new file mode 100644 index 0000000..d705bae --- /dev/null +++ b/HomeWork21/HomeWork21/Models/Pet.cs @@ -0,0 +1,15 @@ +namespace HomeWork21.Models +{ + internal class Pet + { + public int Id { get; set; } + public string Name { get; set; } = null!; + public IEnumerable Category { get; set; } = null!; + public IEnumerable Breed { get; set; } = null!; + public float Age { get; set; } + public IEnumerable Location { get; set; } = null!; + public string? ImageUrl { get; set; } + public string? Discription { get; set; } + + } +} diff --git a/HomeWork21/HomeWork21/Program.cs b/HomeWork21/HomeWork21/Program.cs new file mode 100644 index 0000000..62c8648 --- /dev/null +++ b/HomeWork21/HomeWork21/Program.cs @@ -0,0 +1,46 @@ +using HomeWork21; +using HomeWork21.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +class Program +{ + static async Task Main(string[] args) + { + void ConfigureService(ServiceCollection serviceCollection, IConfiguration confuration) + { + var conectionString = confuration.GetConnectionString("DefaultConnection"); + serviceCollection + .AddDbContext + (o => o.UseSqlServer(conectionString)); + + serviceCollection + .AddLogging(log => log.AddConsole()) + .AddTransient(); + } + + IConfiguration configuration = new ConfigurationBuilder() + .AddJsonFile("config.json") + .Build(); + + var services = new ServiceCollection(); + ConfigureService(services, configuration); + var provider = services.BuildServiceProvider(); + + var migrationSection = configuration.GetSection("Migration"); + var isNeedMigration = migrationSection.GetSection("IsNeedMigration"); + + //dotnet ef migrations add InitialCreate - p HomeWork21 + + if (bool.Parse(isNeedMigration.Value!)) + { + var dbContext = provider.GetService(); + await dbContext!.Database.MigrateAsync(); + } + + var app = provider.GetService(); + await app!.StartUpAsync(); + } +} diff --git a/HomeWork21/HomeWork21/config.json b/HomeWork21/HomeWork21/config.json new file mode 100644 index 0000000..60fc9ed --- /dev/null +++ b/HomeWork21/HomeWork21/config.json @@ -0,0 +1,8 @@ +{ + "Migration": { + "IsNeedMigration": true + }, + "ConnectionStrings": { + "DefaultConnection": "Server=127.0.0.1,1434;Database=master;User ID=sa;Password=1My_password;TrustServerCertificate=true" + } +} From 1531834e708739f15a05c2b0bcd19dca92461378 Mon Sep 17 00:00:00 2001 From: BabychMaksym Date: Tue, 19 Mar 2024 11:34:46 +0200 Subject: [PATCH 2/5] Some change --- HomeWork21/HomeWork21/App.cs | 5 ----- HomeWork21/HomeWork21/Models/Breed.cs | 1 + HomeWork21/HomeWork21/Program.cs | 2 -- 3 files changed, 1 insertion(+), 7 deletions(-) diff --git a/HomeWork21/HomeWork21/App.cs b/HomeWork21/HomeWork21/App.cs index 7e2b8fa..080a4a7 100644 --- a/HomeWork21/HomeWork21/App.cs +++ b/HomeWork21/HomeWork21/App.cs @@ -15,11 +15,6 @@ public App(AplicationDbContext dbContext) public async Task StartUpAsync() { var data = await _dbContext.Pets.ToListAsync(); - foreach (var location in data) - { - Console.WriteLine(location.Id + " " + location.Name); - } - } } } diff --git a/HomeWork21/HomeWork21/Models/Breed.cs b/HomeWork21/HomeWork21/Models/Breed.cs index 131854f..c333258 100644 --- a/HomeWork21/HomeWork21/Models/Breed.cs +++ b/HomeWork21/HomeWork21/Models/Breed.cs @@ -4,5 +4,6 @@ internal class Breed { public int Id { get; set; } public string Name { get; set; } = null!; + public IEnumerable Category { get; set; } = null!; } } diff --git a/HomeWork21/HomeWork21/Program.cs b/HomeWork21/HomeWork21/Program.cs index 62c8648..37164af 100644 --- a/HomeWork21/HomeWork21/Program.cs +++ b/HomeWork21/HomeWork21/Program.cs @@ -32,8 +32,6 @@ void ConfigureService(ServiceCollection serviceCollection, IConfiguration confur var migrationSection = configuration.GetSection("Migration"); var isNeedMigration = migrationSection.GetSection("IsNeedMigration"); - //dotnet ef migrations add InitialCreate - p HomeWork21 - if (bool.Parse(isNeedMigration.Value!)) { var dbContext = provider.GetService(); From 0dc3aaf38608f323becc4a2245eb376420eb8127 Mon Sep 17 00:00:00 2001 From: BabychMaksym Date: Wed, 20 Mar 2024 11:54:33 +0200 Subject: [PATCH 3/5] corect relat --- .../HomeWork21/Data/Entities/BreedEntity.cs | 6 +- .../Data/Entities/CategoryEntity.cs | 9 +- .../Data/Entities/LocationEntity.cs | 5 +- .../HomeWork21/Data/Entities/PetEntity.cs | 9 +- .../EntitiesConfigure/BreedEntityConfigure.cs | 17 +++- .../CategoryEntityConfigure.cs | 16 +-- .../LocationEntityConfigure.cs | 12 +-- .../PetEntityConfiguration.cs | 25 ++++- ... => 20240320095237_initialCre.Designer.cs} | 91 +++++++++-------- ...Create.cs => 20240320095237_initialCre.cs} | 97 +++++++++---------- .../AplicationDbContextModelSnapshot.cs | 87 ++++++++--------- HomeWork21/HomeWork21/Models/Breed.cs | 2 +- HomeWork21/HomeWork21/Models/Pet.cs | 6 +- HomeWork21/HomeWork21/config.json | 2 +- 14 files changed, 195 insertions(+), 189 deletions(-) rename HomeWork21/HomeWork21/Migrations/{20240318034724_InitialCreate.Designer.cs => 20240320095237_initialCre.Designer.cs} (82%) rename HomeWork21/HomeWork21/Migrations/{20240318034724_InitialCreate.cs => 20240320095237_initialCre.cs} (68%) diff --git a/HomeWork21/HomeWork21/Data/Entities/BreedEntity.cs b/HomeWork21/HomeWork21/Data/Entities/BreedEntity.cs index a0a49fd..2f281b1 100644 --- a/HomeWork21/HomeWork21/Data/Entities/BreedEntity.cs +++ b/HomeWork21/HomeWork21/Data/Entities/BreedEntity.cs @@ -4,9 +4,9 @@ internal class BreedEntity { public int Id { get; set; } public string BreedName { get; set; } = null!; - public int PetId { get; set; } - public PetEntity Pet { get; set; } = null!; + public ICollection Pet { get; set; } = new List(); - public List Category { get; set; } = new List(); + public int CategoryId { get; set; } + public CategoryEntity? Category { get; set; } } } diff --git a/HomeWork21/HomeWork21/Data/Entities/CategoryEntity.cs b/HomeWork21/HomeWork21/Data/Entities/CategoryEntity.cs index e0c1885..e3e7e96 100644 --- a/HomeWork21/HomeWork21/Data/Entities/CategoryEntity.cs +++ b/HomeWork21/HomeWork21/Data/Entities/CategoryEntity.cs @@ -4,11 +4,8 @@ internal class CategoryEntity { public int Id { get; set; } public string CategoryName { get; set; } = null!; - - public int PetId { get; set; } - public PetEntity? Pet { get; set; } - - public int BreedId { get; set; } - public BreedEntity? Breed { get; set; } + + public ICollection Pet { get; set; } = new List(); + public ICollection Breed { get; set; } = new List(); } } diff --git a/HomeWork21/HomeWork21/Data/Entities/LocationEntity.cs b/HomeWork21/HomeWork21/Data/Entities/LocationEntity.cs index 9843ce0..dea95aa 100644 --- a/HomeWork21/HomeWork21/Data/Entities/LocationEntity.cs +++ b/HomeWork21/HomeWork21/Data/Entities/LocationEntity.cs @@ -3,9 +3,8 @@ internal class LocationEntity { public int Id { get; set; } - public string? LocationName { get; set; } + public string LocationName { get; set; } = null!; - public int PetId { get; set; } - public PetEntity? Pet { get; set; } + public ICollection Pet { get; set; } = new List(); } } diff --git a/HomeWork21/HomeWork21/Data/Entities/PetEntity.cs b/HomeWork21/HomeWork21/Data/Entities/PetEntity.cs index e4b5ae5..4be588f 100644 --- a/HomeWork21/HomeWork21/Data/Entities/PetEntity.cs +++ b/HomeWork21/HomeWork21/Data/Entities/PetEntity.cs @@ -8,8 +8,11 @@ internal class PetEntity public string? ImageUrl { get; set; } public string? Description { get; set; } - public List Category { get; set; } = new List(); - public List Breed { get; set; } = new List(); - public List LocationName { get; set; } = new List(); + public int CategoryId { get; set; } + public CategoryEntity? Category { get; set; } + public int BreedId { get; set; } + public BreedEntity? Breed { get; set; } + public int LocationId { get; set; } + public LocationEntity? Location { get; set; } } } diff --git a/HomeWork21/HomeWork21/Data/EntitiesConfigure/BreedEntityConfigure.cs b/HomeWork21/HomeWork21/Data/EntitiesConfigure/BreedEntityConfigure.cs index f1ffc20..7b61c5b 100644 --- a/HomeWork21/HomeWork21/Data/EntitiesConfigure/BreedEntityConfigure.cs +++ b/HomeWork21/HomeWork21/Data/EntitiesConfigure/BreedEntityConfigure.cs @@ -9,14 +9,21 @@ internal class BreedEntityConfigure : IEntityTypeConfiguration public void Configure(EntityTypeBuilder builder) { builder.HasKey(x => x.Id); - builder.Property(p => p.BreedName).IsRequired().HasColumnName("breed_name"); - builder.Property(p => p.PetId).IsRequired(); builder - .HasOne(o=>o.Pet) + .Property(p => p.BreedName) + .IsRequired() + .HasColumnName("breed_name"); + + builder + .Property(p => p.CategoryId) + .IsRequired(); + + builder + .HasOne(o => o.Category) .WithMany(m => m.Breed) - .HasForeignKey(o => o.PetId) - .OnDelete(DeleteBehavior.Cascade); + .HasForeignKey(o => o.CategoryId) + .OnDelete(DeleteBehavior.ClientSetNull); } } } diff --git a/HomeWork21/HomeWork21/Data/EntitiesConfigure/CategoryEntityConfigure.cs b/HomeWork21/HomeWork21/Data/EntitiesConfigure/CategoryEntityConfigure.cs index 0ec174b..c958bb6 100644 --- a/HomeWork21/HomeWork21/Data/EntitiesConfigure/CategoryEntityConfigure.cs +++ b/HomeWork21/HomeWork21/Data/EntitiesConfigure/CategoryEntityConfigure.cs @@ -9,21 +9,11 @@ internal class CategoryEntityConfigure : IEntityTypeConfiguration builder) { builder.HasKey(k => k.Id); - builder.Property(p => p.CategoryName).IsRequired().HasColumnName("category_name"); - builder.Property(p => p.PetId).IsRequired(); - builder.Property(p => p.BreedId).IsRequired(); - builder - .HasOne(o=>o.Pet) - .WithMany(m=>m.Category) - .HasForeignKey(o => o.PetId) - .OnDelete(DeleteBehavior.ClientSetNull); + .Property(p => p.CategoryName) + .IsRequired() + .HasColumnName("Category_Name"); - builder - .HasOne(o => o.Breed) - .WithMany(m => m.Category) - .HasForeignKey(k => k.BreedId) - .OnDelete(DeleteBehavior.ClientSetNull); } } } diff --git a/HomeWork21/HomeWork21/Data/EntitiesConfigure/LocationEntityConfigure.cs b/HomeWork21/HomeWork21/Data/EntitiesConfigure/LocationEntityConfigure.cs index aca37ef..ad51432 100644 --- a/HomeWork21/HomeWork21/Data/EntitiesConfigure/LocationEntityConfigure.cs +++ b/HomeWork21/HomeWork21/Data/EntitiesConfigure/LocationEntityConfigure.cs @@ -8,20 +8,12 @@ internal class LocationEntityConfigure : IEntityTypeConfiguration builder) { - builder.HasKey(k => k.Id); - builder.Property(p=>p.PetId).IsRequired(); + builder.HasKey(k => k.Id); builder .Property(p => p.LocationName) .IsRequired() - .HasColumnName("location_name"); - - - builder - .HasOne(o => o.Pet) - .WithMany(o => o.LocationName) - .HasForeignKey(k => k.PetId) - .OnDelete(DeleteBehavior.Cascade); + .HasColumnName("Location_Name"); } } } diff --git a/HomeWork21/HomeWork21/Data/EntitiesConfigure/PetEntityConfiguration.cs b/HomeWork21/HomeWork21/Data/EntitiesConfigure/PetEntityConfiguration.cs index 3605303..8bb5495 100644 --- a/HomeWork21/HomeWork21/Data/EntitiesConfigure/PetEntityConfiguration.cs +++ b/HomeWork21/HomeWork21/Data/EntitiesConfigure/PetEntityConfiguration.cs @@ -11,8 +11,29 @@ public void Configure(EntityTypeBuilder builder) builder.HasKey(k => k.Id); builder.Property(p=>p.Name).IsRequired(); builder.Property(p => p.Age).IsRequired(); - builder.Property(p => p.ImageUrl).HasColumnName("image_url"); - builder.Property(p => p.Description); + builder.Property(p => p.ImageUrl).HasColumnName("Image_Url"); + builder.Property(p => p.Description); + builder.Property(p => p.CategoryId).IsRequired(); + builder.Property(p => p.BreedId).IsRequired(); + builder.Property(p => p.LocationId).IsRequired(); + + builder + .HasOne(o => o.Category) + .WithMany(m => m.Pet) + .HasForeignKey(o => o.CategoryId) + .OnDelete(DeleteBehavior.ClientSetNull); + + builder + .HasOne(o => o.Breed) + .WithMany(m => m.Pet) + .HasForeignKey(o => o.BreedId) + .OnDelete(DeleteBehavior.ClientSetNull); + + builder + .HasOne(o => o.Location) + .WithMany(m => m.Pet) + .HasForeignKey(o => o.LocationId) + .OnDelete(DeleteBehavior.Cascade); } } } diff --git a/HomeWork21/HomeWork21/Migrations/20240318034724_InitialCreate.Designer.cs b/HomeWork21/HomeWork21/Migrations/20240320095237_initialCre.Designer.cs similarity index 82% rename from HomeWork21/HomeWork21/Migrations/20240318034724_InitialCreate.Designer.cs rename to HomeWork21/HomeWork21/Migrations/20240320095237_initialCre.Designer.cs index 726e228..6616683 100644 --- a/HomeWork21/HomeWork21/Migrations/20240318034724_InitialCreate.Designer.cs +++ b/HomeWork21/HomeWork21/Migrations/20240320095237_initialCre.Designer.cs @@ -11,8 +11,8 @@ namespace HomeWork21.Migrations { [DbContext(typeof(AplicationDbContext))] - [Migration("20240318034724_InitialCreate")] - partial class InitialCreate + [Migration("20240320095237_initialCre")] + partial class initialCre { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -40,12 +40,12 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .HasColumnType("nvarchar(max)") .HasColumnName("breed_name"); - b.Property("PetId") + b.Property("CategoryId") .HasColumnType("int"); b.HasKey("Id"); - b.HasIndex("PetId"); + b.HasIndex("CategoryId"); b.ToTable("Breeds"); }); @@ -58,23 +58,13 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); - b.Property("BreedId") - .HasColumnType("int"); - b.Property("CategoryName") .IsRequired() .HasColumnType("nvarchar(max)") - .HasColumnName("category_name"); - - b.Property("PetId") - .HasColumnType("int"); + .HasColumnName("Category_Name"); b.HasKey("Id"); - b.HasIndex("BreedId"); - - b.HasIndex("PetId"); - b.ToTable("Categories"); }); @@ -89,15 +79,10 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("LocationName") .IsRequired() .HasColumnType("nvarchar(max)") - .HasColumnName("location_name"); - - b.Property("PetId") - .HasColumnType("int"); + .HasColumnName("Location_Name"); b.HasKey("Id"); - b.HasIndex("PetId"); - b.ToTable("Locations"); }); @@ -112,12 +97,21 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("Age") .HasColumnType("real"); + b.Property("BreedId") + .HasColumnType("int"); + + b.Property("CategoryId") + .HasColumnType("int"); + b.Property("Description") .HasColumnType("nvarchar(max)"); b.Property("ImageUrl") .HasColumnType("nvarchar(max)") - .HasColumnName("image_url"); + .HasColumnName("Image_Url"); + + b.Property("LocationId") + .HasColumnType("int"); b.Property("Name") .IsRequired() @@ -125,60 +119,65 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.HasKey("Id"); + b.HasIndex("BreedId"); + + b.HasIndex("CategoryId"); + + b.HasIndex("LocationId"); + b.ToTable("Pets"); }); modelBuilder.Entity("HomeWork21.Data.Entities.BreedEntity", b => { - b.HasOne("HomeWork21.Data.Entities.PetEntity", "Pet") + b.HasOne("HomeWork21.Data.Entities.CategoryEntity", "Category") .WithMany("Breed") - .HasForeignKey("PetId") - .OnDelete(DeleteBehavior.Cascade) + .HasForeignKey("CategoryId") .IsRequired(); - b.Navigation("Pet"); + b.Navigation("Category"); }); - modelBuilder.Entity("HomeWork21.Data.Entities.CategoryEntity", b => + modelBuilder.Entity("HomeWork21.Data.Entities.PetEntity", b => { b.HasOne("HomeWork21.Data.Entities.BreedEntity", "Breed") - .WithMany("Category") + .WithMany("Pet") .HasForeignKey("BreedId") .IsRequired(); - b.HasOne("HomeWork21.Data.Entities.PetEntity", "Pet") - .WithMany("Category") - .HasForeignKey("PetId") + b.HasOne("HomeWork21.Data.Entities.CategoryEntity", "Category") + .WithMany("Pet") + .HasForeignKey("CategoryId") .IsRequired(); - b.Navigation("Breed"); - - b.Navigation("Pet"); - }); - - modelBuilder.Entity("HomeWork21.Data.Entities.LocationEntity", b => - { - b.HasOne("HomeWork21.Data.Entities.PetEntity", "Pet") - .WithMany("LocationName") - .HasForeignKey("PetId") + b.HasOne("HomeWork21.Data.Entities.LocationEntity", "Location") + .WithMany("Pet") + .HasForeignKey("LocationId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.Navigation("Pet"); + b.Navigation("Breed"); + + b.Navigation("Category"); + + b.Navigation("Location"); }); modelBuilder.Entity("HomeWork21.Data.Entities.BreedEntity", b => { - b.Navigation("Category"); + b.Navigation("Pet"); }); - modelBuilder.Entity("HomeWork21.Data.Entities.PetEntity", b => + modelBuilder.Entity("HomeWork21.Data.Entities.CategoryEntity", b => { b.Navigation("Breed"); - b.Navigation("Category"); + b.Navigation("Pet"); + }); - b.Navigation("LocationName"); + modelBuilder.Entity("HomeWork21.Data.Entities.LocationEntity", b => + { + b.Navigation("Pet"); }); #pragma warning restore 612, 618 } diff --git a/HomeWork21/HomeWork21/Migrations/20240318034724_InitialCreate.cs b/HomeWork21/HomeWork21/Migrations/20240320095237_initialCre.cs similarity index 68% rename from HomeWork21/HomeWork21/Migrations/20240318034724_InitialCreate.cs rename to HomeWork21/HomeWork21/Migrations/20240320095237_initialCre.cs index 25fd643..c4ebe70 100644 --- a/HomeWork21/HomeWork21/Migrations/20240318034724_InitialCreate.cs +++ b/HomeWork21/HomeWork21/Migrations/20240320095237_initialCre.cs @@ -5,7 +5,7 @@ namespace HomeWork21.Migrations { /// - public partial class InitialCreate : Migration + public partial class initialCre : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -15,117 +15,116 @@ protected override void Up(MigrationBuilder migrationBuilder) incrementBy: 10); migrationBuilder.CreateTable( - name: "Pets", + name: "Categories", columns: table => new { Id = table.Column(type: "int", nullable: false), - Name = table.Column(type: "nvarchar(max)", nullable: false), - Age = table.Column(type: "real", nullable: false), - image_url = table.Column(type: "nvarchar(max)", nullable: true), - Description = table.Column(type: "nvarchar(max)", nullable: true) + Category_Name = table.Column(type: "nvarchar(max)", nullable: false) }, constraints: table => { - table.PrimaryKey("PK_Pets", x => x.Id); + table.PrimaryKey("PK_Categories", x => x.Id); }); migrationBuilder.CreateTable( - name: "Breeds", + name: "Locations", columns: table => new { Id = table.Column(type: "int", nullable: false), - breed_name = table.Column(type: "nvarchar(max)", nullable: false), - PetId = table.Column(type: "int", nullable: false) + Location_Name = table.Column(type: "nvarchar(max)", nullable: false) }, constraints: table => { - table.PrimaryKey("PK_Breeds", x => x.Id); - table.ForeignKey( - name: "FK_Breeds_Pets_PetId", - column: x => x.PetId, - principalTable: "Pets", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); + table.PrimaryKey("PK_Locations", x => x.Id); }); migrationBuilder.CreateTable( - name: "Locations", + name: "Breeds", columns: table => new { Id = table.Column(type: "int", nullable: false), - location_name = table.Column(type: "nvarchar(max)", nullable: false), - PetId = table.Column(type: "int", nullable: false) + breed_name = table.Column(type: "nvarchar(max)", nullable: false), + CategoryId = table.Column(type: "int", nullable: false) }, constraints: table => { - table.PrimaryKey("PK_Locations", x => x.Id); + table.PrimaryKey("PK_Breeds", x => x.Id); table.ForeignKey( - name: "FK_Locations_Pets_PetId", - column: x => x.PetId, - principalTable: "Pets", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); + name: "FK_Breeds_Categories_CategoryId", + column: x => x.CategoryId, + principalTable: "Categories", + principalColumn: "Id"); }); migrationBuilder.CreateTable( - name: "Categories", + name: "Pets", columns: table => new { Id = table.Column(type: "int", nullable: false), - category_name = table.Column(type: "nvarchar(max)", nullable: false), - PetId = table.Column(type: "int", nullable: false), - BreedId = table.Column(type: "int", nullable: false) + Name = table.Column(type: "nvarchar(max)", nullable: false), + Age = table.Column(type: "real", nullable: false), + Image_Url = table.Column(type: "nvarchar(max)", nullable: true), + Description = table.Column(type: "nvarchar(max)", nullable: true), + CategoryId = table.Column(type: "int", nullable: false), + BreedId = table.Column(type: "int", nullable: false), + LocationId = table.Column(type: "int", nullable: false) }, constraints: table => { - table.PrimaryKey("PK_Categories", x => x.Id); + table.PrimaryKey("PK_Pets", x => x.Id); table.ForeignKey( - name: "FK_Categories_Breeds_BreedId", + name: "FK_Pets_Breeds_BreedId", column: x => x.BreedId, principalTable: "Breeds", principalColumn: "Id"); table.ForeignKey( - name: "FK_Categories_Pets_PetId", - column: x => x.PetId, - principalTable: "Pets", + name: "FK_Pets_Categories_CategoryId", + column: x => x.CategoryId, + principalTable: "Categories", principalColumn: "Id"); + table.ForeignKey( + name: "FK_Pets_Locations_LocationId", + column: x => x.LocationId, + principalTable: "Locations", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateIndex( - name: "IX_Breeds_PetId", + name: "IX_Breeds_CategoryId", table: "Breeds", - column: "PetId"); + column: "CategoryId"); migrationBuilder.CreateIndex( - name: "IX_Categories_BreedId", - table: "Categories", + name: "IX_Pets_BreedId", + table: "Pets", column: "BreedId"); migrationBuilder.CreateIndex( - name: "IX_Categories_PetId", - table: "Categories", - column: "PetId"); + name: "IX_Pets_CategoryId", + table: "Pets", + column: "CategoryId"); migrationBuilder.CreateIndex( - name: "IX_Locations_PetId", - table: "Locations", - column: "PetId"); + name: "IX_Pets_LocationId", + table: "Pets", + column: "LocationId"); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( - name: "Categories"); + name: "Pets"); migrationBuilder.DropTable( - name: "Locations"); + name: "Breeds"); migrationBuilder.DropTable( - name: "Breeds"); + name: "Locations"); migrationBuilder.DropTable( - name: "Pets"); + name: "Categories"); migrationBuilder.DropSequence( name: "EntityFrameworkHiLoSequence"); diff --git a/HomeWork21/HomeWork21/Migrations/AplicationDbContextModelSnapshot.cs b/HomeWork21/HomeWork21/Migrations/AplicationDbContextModelSnapshot.cs index 973c6c7..8b24718 100644 --- a/HomeWork21/HomeWork21/Migrations/AplicationDbContextModelSnapshot.cs +++ b/HomeWork21/HomeWork21/Migrations/AplicationDbContextModelSnapshot.cs @@ -37,12 +37,12 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasColumnType("nvarchar(max)") .HasColumnName("breed_name"); - b.Property("PetId") + b.Property("CategoryId") .HasColumnType("int"); b.HasKey("Id"); - b.HasIndex("PetId"); + b.HasIndex("CategoryId"); b.ToTable("Breeds"); }); @@ -55,23 +55,13 @@ protected override void BuildModel(ModelBuilder modelBuilder) SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); - b.Property("BreedId") - .HasColumnType("int"); - b.Property("CategoryName") .IsRequired() .HasColumnType("nvarchar(max)") - .HasColumnName("category_name"); - - b.Property("PetId") - .HasColumnType("int"); + .HasColumnName("Category_Name"); b.HasKey("Id"); - b.HasIndex("BreedId"); - - b.HasIndex("PetId"); - b.ToTable("Categories"); }); @@ -86,15 +76,10 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("LocationName") .IsRequired() .HasColumnType("nvarchar(max)") - .HasColumnName("location_name"); - - b.Property("PetId") - .HasColumnType("int"); + .HasColumnName("Location_Name"); b.HasKey("Id"); - b.HasIndex("PetId"); - b.ToTable("Locations"); }); @@ -109,12 +94,21 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("Age") .HasColumnType("real"); + b.Property("BreedId") + .HasColumnType("int"); + + b.Property("CategoryId") + .HasColumnType("int"); + b.Property("Description") .HasColumnType("nvarchar(max)"); b.Property("ImageUrl") .HasColumnType("nvarchar(max)") - .HasColumnName("image_url"); + .HasColumnName("Image_Url"); + + b.Property("LocationId") + .HasColumnType("int"); b.Property("Name") .IsRequired() @@ -122,60 +116,65 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasKey("Id"); + b.HasIndex("BreedId"); + + b.HasIndex("CategoryId"); + + b.HasIndex("LocationId"); + b.ToTable("Pets"); }); modelBuilder.Entity("HomeWork21.Data.Entities.BreedEntity", b => { - b.HasOne("HomeWork21.Data.Entities.PetEntity", "Pet") + b.HasOne("HomeWork21.Data.Entities.CategoryEntity", "Category") .WithMany("Breed") - .HasForeignKey("PetId") - .OnDelete(DeleteBehavior.Cascade) + .HasForeignKey("CategoryId") .IsRequired(); - b.Navigation("Pet"); + b.Navigation("Category"); }); - modelBuilder.Entity("HomeWork21.Data.Entities.CategoryEntity", b => + modelBuilder.Entity("HomeWork21.Data.Entities.PetEntity", b => { b.HasOne("HomeWork21.Data.Entities.BreedEntity", "Breed") - .WithMany("Category") + .WithMany("Pet") .HasForeignKey("BreedId") .IsRequired(); - b.HasOne("HomeWork21.Data.Entities.PetEntity", "Pet") - .WithMany("Category") - .HasForeignKey("PetId") + b.HasOne("HomeWork21.Data.Entities.CategoryEntity", "Category") + .WithMany("Pet") + .HasForeignKey("CategoryId") .IsRequired(); - b.Navigation("Breed"); - - b.Navigation("Pet"); - }); - - modelBuilder.Entity("HomeWork21.Data.Entities.LocationEntity", b => - { - b.HasOne("HomeWork21.Data.Entities.PetEntity", "Pet") - .WithMany("LocationName") - .HasForeignKey("PetId") + b.HasOne("HomeWork21.Data.Entities.LocationEntity", "Location") + .WithMany("Pet") + .HasForeignKey("LocationId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.Navigation("Pet"); + b.Navigation("Breed"); + + b.Navigation("Category"); + + b.Navigation("Location"); }); modelBuilder.Entity("HomeWork21.Data.Entities.BreedEntity", b => { - b.Navigation("Category"); + b.Navigation("Pet"); }); - modelBuilder.Entity("HomeWork21.Data.Entities.PetEntity", b => + modelBuilder.Entity("HomeWork21.Data.Entities.CategoryEntity", b => { b.Navigation("Breed"); - b.Navigation("Category"); + b.Navigation("Pet"); + }); - b.Navigation("LocationName"); + modelBuilder.Entity("HomeWork21.Data.Entities.LocationEntity", b => + { + b.Navigation("Pet"); }); #pragma warning restore 612, 618 } diff --git a/HomeWork21/HomeWork21/Models/Breed.cs b/HomeWork21/HomeWork21/Models/Breed.cs index c333258..6f1c564 100644 --- a/HomeWork21/HomeWork21/Models/Breed.cs +++ b/HomeWork21/HomeWork21/Models/Breed.cs @@ -4,6 +4,6 @@ internal class Breed { public int Id { get; set; } public string Name { get; set; } = null!; - public IEnumerable Category { get; set; } = null!; + public int CategoryId { get; set; } } } diff --git a/HomeWork21/HomeWork21/Models/Pet.cs b/HomeWork21/HomeWork21/Models/Pet.cs index d705bae..3b557e9 100644 --- a/HomeWork21/HomeWork21/Models/Pet.cs +++ b/HomeWork21/HomeWork21/Models/Pet.cs @@ -4,10 +4,10 @@ internal class Pet { public int Id { get; set; } public string Name { get; set; } = null!; - public IEnumerable Category { get; set; } = null!; - public IEnumerable Breed { get; set; } = null!; + public int CategoryId { get; set; } + public int BreedId { get; set; } public float Age { get; set; } - public IEnumerable Location { get; set; } = null!; + public int LocationId { get; set; } public string? ImageUrl { get; set; } public string? Discription { get; set; } diff --git a/HomeWork21/HomeWork21/config.json b/HomeWork21/HomeWork21/config.json index 60fc9ed..463a066 100644 --- a/HomeWork21/HomeWork21/config.json +++ b/HomeWork21/HomeWork21/config.json @@ -3,6 +3,6 @@ "IsNeedMigration": true }, "ConnectionStrings": { - "DefaultConnection": "Server=127.0.0.1,1434;Database=master;User ID=sa;Password=1My_password;TrustServerCertificate=true" + "DefaultConnection": "Server=127.0.0.1,1434;Database=AdventureWorks;User ID=sa;Password=1My_password;TrustServerCertificate=true" } } From 39a1deb69937ae3ad64dfa68bca651f337942199 Mon Sep 17 00:00:00 2001 From: BabychMaksym Date: Wed, 20 Mar 2024 12:40:53 +0200 Subject: [PATCH 4/5] add finale changes)) --- .../Data/EntitiesConfigure/BreedEntityConfigure.cs | 2 +- .../Data/EntitiesConfigure/PetEntityConfiguration.cs | 2 +- ...e.Designer.cs => 20240320103851_ItialCreate.Designer.cs} | 4 ++-- ...20095237_initialCre.cs => 20240320103851_ItialCreate.cs} | 2 +- HomeWork21/HomeWork21/Models/Pet.cs | 6 +++--- 5 files changed, 8 insertions(+), 8 deletions(-) rename HomeWork21/HomeWork21/Migrations/{20240320095237_initialCre.Designer.cs => 20240320103851_ItialCreate.Designer.cs} (98%) rename HomeWork21/HomeWork21/Migrations/{20240320095237_initialCre.cs => 20240320103851_ItialCreate.cs} (99%) diff --git a/HomeWork21/HomeWork21/Data/EntitiesConfigure/BreedEntityConfigure.cs b/HomeWork21/HomeWork21/Data/EntitiesConfigure/BreedEntityConfigure.cs index 7b61c5b..846c271 100644 --- a/HomeWork21/HomeWork21/Data/EntitiesConfigure/BreedEntityConfigure.cs +++ b/HomeWork21/HomeWork21/Data/EntitiesConfigure/BreedEntityConfigure.cs @@ -23,7 +23,7 @@ public void Configure(EntityTypeBuilder builder) .HasOne(o => o.Category) .WithMany(m => m.Breed) .HasForeignKey(o => o.CategoryId) - .OnDelete(DeleteBehavior.ClientSetNull); + .OnDelete(DeleteBehavior.Cascade); } } } diff --git a/HomeWork21/HomeWork21/Data/EntitiesConfigure/PetEntityConfiguration.cs b/HomeWork21/HomeWork21/Data/EntitiesConfigure/PetEntityConfiguration.cs index 8bb5495..bc9fe52 100644 --- a/HomeWork21/HomeWork21/Data/EntitiesConfigure/PetEntityConfiguration.cs +++ b/HomeWork21/HomeWork21/Data/EntitiesConfigure/PetEntityConfiguration.cs @@ -21,7 +21,7 @@ public void Configure(EntityTypeBuilder builder) .HasOne(o => o.Category) .WithMany(m => m.Pet) .HasForeignKey(o => o.CategoryId) - .OnDelete(DeleteBehavior.ClientSetNull); + .OnDelete(DeleteBehavior.Cascade); builder .HasOne(o => o.Breed) diff --git a/HomeWork21/HomeWork21/Migrations/20240320095237_initialCre.Designer.cs b/HomeWork21/HomeWork21/Migrations/20240320103851_ItialCreate.Designer.cs similarity index 98% rename from HomeWork21/HomeWork21/Migrations/20240320095237_initialCre.Designer.cs rename to HomeWork21/HomeWork21/Migrations/20240320103851_ItialCreate.Designer.cs index 6616683..0953bd5 100644 --- a/HomeWork21/HomeWork21/Migrations/20240320095237_initialCre.Designer.cs +++ b/HomeWork21/HomeWork21/Migrations/20240320103851_ItialCreate.Designer.cs @@ -11,8 +11,8 @@ namespace HomeWork21.Migrations { [DbContext(typeof(AplicationDbContext))] - [Migration("20240320095237_initialCre")] - partial class initialCre + [Migration("20240320103851_ItialCreate")] + partial class ItialCreate { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) diff --git a/HomeWork21/HomeWork21/Migrations/20240320095237_initialCre.cs b/HomeWork21/HomeWork21/Migrations/20240320103851_ItialCreate.cs similarity index 99% rename from HomeWork21/HomeWork21/Migrations/20240320095237_initialCre.cs rename to HomeWork21/HomeWork21/Migrations/20240320103851_ItialCreate.cs index c4ebe70..f1fbac0 100644 --- a/HomeWork21/HomeWork21/Migrations/20240320095237_initialCre.cs +++ b/HomeWork21/HomeWork21/Migrations/20240320103851_ItialCreate.cs @@ -5,7 +5,7 @@ namespace HomeWork21.Migrations { /// - public partial class initialCre : Migration + public partial class ItialCreate : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) diff --git a/HomeWork21/HomeWork21/Models/Pet.cs b/HomeWork21/HomeWork21/Models/Pet.cs index 3b557e9..2fd80cf 100644 --- a/HomeWork21/HomeWork21/Models/Pet.cs +++ b/HomeWork21/HomeWork21/Models/Pet.cs @@ -4,10 +4,10 @@ internal class Pet { public int Id { get; set; } public string Name { get; set; } = null!; - public int CategoryId { get; set; } - public int BreedId { get; set; } + public Category? Category { get; set; } + public Breed? Breed { get; set; } public float Age { get; set; } - public int LocationId { get; set; } + public Location? Location { get; set; } public string? ImageUrl { get; set; } public string? Discription { get; set; } From ebd3749517a09501e14332b624c17f452421cabf Mon Sep 17 00:00:00 2001 From: BabychMaksym Date: Thu, 21 Mar 2024 10:08:11 +0200 Subject: [PATCH 5/5] some ok) --- HomeWork21/HomeWork21/App.cs | 42 ++++ .../EntitiesConfigure/BreedEntityConfigure.cs | 5 +- .../PetEntityConfiguration.cs | 8 +- HomeWork21/HomeWork21/HomeWork21.csproj | 4 +- .../20240320191921_AddTablesPet.Designer.cs | 187 +++++++++++++++ .../Migrations/20240320191921_AddTablesPet.cs | 182 ++++++++++++++ .../20240321040818_ColumName.Designer.cs | 191 +++++++++++++++ .../Migrations/20240321040818_ColumName.cs | 226 ++++++++++++++++++ .../20240321041029_ColumNameN.Designer.cs | 191 +++++++++++++++ .../Migrations/20240321041029_ColumNameN.cs | 28 +++ .../AplicationDbContextModelSnapshot.cs | 18 +- HomeWork21/HomeWork21/Models/Breed.cs | 2 +- 12 files changed, 1071 insertions(+), 13 deletions(-) create mode 100644 HomeWork21/HomeWork21/Migrations/20240320191921_AddTablesPet.Designer.cs create mode 100644 HomeWork21/HomeWork21/Migrations/20240320191921_AddTablesPet.cs create mode 100644 HomeWork21/HomeWork21/Migrations/20240321040818_ColumName.Designer.cs create mode 100644 HomeWork21/HomeWork21/Migrations/20240321040818_ColumName.cs create mode 100644 HomeWork21/HomeWork21/Migrations/20240321041029_ColumNameN.Designer.cs create mode 100644 HomeWork21/HomeWork21/Migrations/20240321041029_ColumNameN.cs diff --git a/HomeWork21/HomeWork21/App.cs b/HomeWork21/HomeWork21/App.cs index 080a4a7..59d31a0 100644 --- a/HomeWork21/HomeWork21/App.cs +++ b/HomeWork21/HomeWork21/App.cs @@ -1,4 +1,5 @@ using HomeWork21.Data; +using HomeWork21.Models; using Microsoft.EntityFrameworkCore; namespace HomeWork21 @@ -14,7 +15,48 @@ public App(AplicationDbContext dbContext) public async Task StartUpAsync() { + + List pets = new List(); + var data = await _dbContext.Pets.ToListAsync(); + + foreach (var pet in data) + { + pets.Add( + new Pet() + { + Id = pet.Id, + Name = pet.Name, + Age = pet.Age, + Discription = pet.Description, + ImageUrl = pet.ImageUrl, + + Category = new Category() + { + Id = pet.Category.Id, + Name = pet.Category.CategoryName, + }, + + Breed = new Breed() + { + Id = pet.Breed.Id, + Name = pet.Breed.BreedName, + + Category = new Category() + { + Id = pet.Category.Id, + Name = pet.Category.CategoryName, + }, + }, + + Location = new Location() + { + Id = pet.Location.Id, + Name = pet.Location.LocationName + } + + }); + } } } } diff --git a/HomeWork21/HomeWork21/Data/EntitiesConfigure/BreedEntityConfigure.cs b/HomeWork21/HomeWork21/Data/EntitiesConfigure/BreedEntityConfigure.cs index 846c271..605e214 100644 --- a/HomeWork21/HomeWork21/Data/EntitiesConfigure/BreedEntityConfigure.cs +++ b/HomeWork21/HomeWork21/Data/EntitiesConfigure/BreedEntityConfigure.cs @@ -13,11 +13,12 @@ public void Configure(EntityTypeBuilder builder) builder .Property(p => p.BreedName) .IsRequired() - .HasColumnName("breed_name"); + .HasColumnName("Breed_Name"); builder .Property(p => p.CategoryId) - .IsRequired(); + .IsRequired() + .HasColumnName("Category_Id"); builder .HasOne(o => o.Category) diff --git a/HomeWork21/HomeWork21/Data/EntitiesConfigure/PetEntityConfiguration.cs b/HomeWork21/HomeWork21/Data/EntitiesConfigure/PetEntityConfiguration.cs index bc9fe52..727f1e8 100644 --- a/HomeWork21/HomeWork21/Data/EntitiesConfigure/PetEntityConfiguration.cs +++ b/HomeWork21/HomeWork21/Data/EntitiesConfigure/PetEntityConfiguration.cs @@ -8,14 +8,16 @@ internal class PetEntityConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder builder) { + builder.ToTable("Pet", "dbo"); + builder.HasKey(k => k.Id); builder.Property(p=>p.Name).IsRequired(); builder.Property(p => p.Age).IsRequired(); builder.Property(p => p.ImageUrl).HasColumnName("Image_Url"); builder.Property(p => p.Description); - builder.Property(p => p.CategoryId).IsRequired(); - builder.Property(p => p.BreedId).IsRequired(); - builder.Property(p => p.LocationId).IsRequired(); + builder.Property(p => p.CategoryId).IsRequired().HasColumnName("Category_Id"); + builder.Property(p => p.BreedId).IsRequired().HasColumnName("Breed_Id"); + builder.Property(p => p.LocationId).IsRequired().HasColumnName("Location_Id"); builder .HasOne(o => o.Category) diff --git a/HomeWork21/HomeWork21/HomeWork21.csproj b/HomeWork21/HomeWork21/HomeWork21.csproj index 6c7480b..5cb3045 100644 --- a/HomeWork21/HomeWork21/HomeWork21.csproj +++ b/HomeWork21/HomeWork21/HomeWork21.csproj @@ -9,7 +9,9 @@ - + + Always + diff --git a/HomeWork21/HomeWork21/Migrations/20240320191921_AddTablesPet.Designer.cs b/HomeWork21/HomeWork21/Migrations/20240320191921_AddTablesPet.Designer.cs new file mode 100644 index 0000000..b83af59 --- /dev/null +++ b/HomeWork21/HomeWork21/Migrations/20240320191921_AddTablesPet.Designer.cs @@ -0,0 +1,187 @@ +// +using HomeWork21.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace HomeWork21.Migrations +{ + [DbContext(typeof(AplicationDbContext))] + [Migration("20240320191921_AddTablesPet")] + partial class AddTablesPet + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseHiLo(modelBuilder, "EntityFrameworkHiLoSequence"); + + modelBuilder.HasSequence("EntityFrameworkHiLoSequence") + .IncrementsBy(10); + + modelBuilder.Entity("HomeWork21.Data.Entities.BreedEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("BreedName") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("breed_name"); + + b.Property("CategoryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("CategoryId"); + + b.ToTable("Breeds"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.CategoryEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("CategoryName") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("Category_Name"); + + b.HasKey("Id"); + + b.ToTable("Categories"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.LocationEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("LocationName") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("Location_Name"); + + b.HasKey("Id"); + + b.ToTable("Locations"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.PetEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("Age") + .HasColumnType("real"); + + b.Property("BreedId") + .HasColumnType("int"); + + b.Property("CategoryId") + .HasColumnType("int"); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("ImageUrl") + .HasColumnType("nvarchar(max)") + .HasColumnName("Image_Url"); + + b.Property("LocationId") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("BreedId"); + + b.HasIndex("CategoryId"); + + b.HasIndex("LocationId"); + + b.ToTable("Pet", "dbo"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.BreedEntity", b => + { + b.HasOne("HomeWork21.Data.Entities.CategoryEntity", "Category") + .WithMany("Breed") + .HasForeignKey("CategoryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Category"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.PetEntity", b => + { + b.HasOne("HomeWork21.Data.Entities.BreedEntity", "Breed") + .WithMany("Pet") + .HasForeignKey("BreedId") + .IsRequired(); + + b.HasOne("HomeWork21.Data.Entities.CategoryEntity", "Category") + .WithMany("Pet") + .HasForeignKey("CategoryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("HomeWork21.Data.Entities.LocationEntity", "Location") + .WithMany("Pet") + .HasForeignKey("LocationId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Breed"); + + b.Navigation("Category"); + + b.Navigation("Location"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.BreedEntity", b => + { + b.Navigation("Pet"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.CategoryEntity", b => + { + b.Navigation("Breed"); + + b.Navigation("Pet"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.LocationEntity", b => + { + b.Navigation("Pet"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/HomeWork21/HomeWork21/Migrations/20240320191921_AddTablesPet.cs b/HomeWork21/HomeWork21/Migrations/20240320191921_AddTablesPet.cs new file mode 100644 index 0000000..186131c --- /dev/null +++ b/HomeWork21/HomeWork21/Migrations/20240320191921_AddTablesPet.cs @@ -0,0 +1,182 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace HomeWork21.Migrations +{ + /// + public partial class AddTablesPet : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Breeds_Categories_CategoryId", + table: "Breeds"); + + migrationBuilder.DropForeignKey( + name: "FK_Pets_Breeds_BreedId", + table: "Pets"); + + migrationBuilder.DropForeignKey( + name: "FK_Pets_Categories_CategoryId", + table: "Pets"); + + migrationBuilder.DropForeignKey( + name: "FK_Pets_Locations_LocationId", + table: "Pets"); + + migrationBuilder.DropPrimaryKey( + name: "PK_Pets", + table: "Pets"); + + migrationBuilder.EnsureSchema( + name: "dbo"); + + migrationBuilder.RenameTable( + name: "Pets", + newName: "Pet", + newSchema: "dbo"); + + migrationBuilder.RenameIndex( + name: "IX_Pets_LocationId", + schema: "dbo", + table: "Pet", + newName: "IX_Pet_LocationId"); + + migrationBuilder.RenameIndex( + name: "IX_Pets_CategoryId", + schema: "dbo", + table: "Pet", + newName: "IX_Pet_CategoryId"); + + migrationBuilder.RenameIndex( + name: "IX_Pets_BreedId", + schema: "dbo", + table: "Pet", + newName: "IX_Pet_BreedId"); + + migrationBuilder.AddPrimaryKey( + name: "PK_Pet", + schema: "dbo", + table: "Pet", + column: "Id"); + + migrationBuilder.AddForeignKey( + name: "FK_Breeds_Categories_CategoryId", + table: "Breeds", + column: "CategoryId", + principalTable: "Categories", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Pet_Breeds_BreedId", + schema: "dbo", + table: "Pet", + column: "BreedId", + principalTable: "Breeds", + principalColumn: "Id"); + + migrationBuilder.AddForeignKey( + name: "FK_Pet_Categories_CategoryId", + schema: "dbo", + table: "Pet", + column: "CategoryId", + principalTable: "Categories", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Pet_Locations_LocationId", + schema: "dbo", + table: "Pet", + column: "LocationId", + principalTable: "Locations", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Breeds_Categories_CategoryId", + table: "Breeds"); + + migrationBuilder.DropForeignKey( + name: "FK_Pet_Breeds_BreedId", + schema: "dbo", + table: "Pet"); + + migrationBuilder.DropForeignKey( + name: "FK_Pet_Categories_CategoryId", + schema: "dbo", + table: "Pet"); + + migrationBuilder.DropForeignKey( + name: "FK_Pet_Locations_LocationId", + schema: "dbo", + table: "Pet"); + + migrationBuilder.DropPrimaryKey( + name: "PK_Pet", + schema: "dbo", + table: "Pet"); + + migrationBuilder.RenameTable( + name: "Pet", + schema: "dbo", + newName: "Pets"); + + migrationBuilder.RenameIndex( + name: "IX_Pet_LocationId", + table: "Pets", + newName: "IX_Pets_LocationId"); + + migrationBuilder.RenameIndex( + name: "IX_Pet_CategoryId", + table: "Pets", + newName: "IX_Pets_CategoryId"); + + migrationBuilder.RenameIndex( + name: "IX_Pet_BreedId", + table: "Pets", + newName: "IX_Pets_BreedId"); + + migrationBuilder.AddPrimaryKey( + name: "PK_Pets", + table: "Pets", + column: "Id"); + + migrationBuilder.AddForeignKey( + name: "FK_Breeds_Categories_CategoryId", + table: "Breeds", + column: "CategoryId", + principalTable: "Categories", + principalColumn: "Id"); + + migrationBuilder.AddForeignKey( + name: "FK_Pets_Breeds_BreedId", + table: "Pets", + column: "BreedId", + principalTable: "Breeds", + principalColumn: "Id"); + + migrationBuilder.AddForeignKey( + name: "FK_Pets_Categories_CategoryId", + table: "Pets", + column: "CategoryId", + principalTable: "Categories", + principalColumn: "Id"); + + migrationBuilder.AddForeignKey( + name: "FK_Pets_Locations_LocationId", + table: "Pets", + column: "LocationId", + principalTable: "Locations", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + } +} diff --git a/HomeWork21/HomeWork21/Migrations/20240321040818_ColumName.Designer.cs b/HomeWork21/HomeWork21/Migrations/20240321040818_ColumName.Designer.cs new file mode 100644 index 0000000..00d7b16 --- /dev/null +++ b/HomeWork21/HomeWork21/Migrations/20240321040818_ColumName.Designer.cs @@ -0,0 +1,191 @@ +// +using HomeWork21.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace HomeWork21.Migrations +{ + [DbContext(typeof(AplicationDbContext))] + [Migration("20240321040818_ColumName")] + partial class ColumName + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseHiLo(modelBuilder, "EntityFrameworkHiLoSequence"); + + modelBuilder.HasSequence("EntityFrameworkHiLoSequence") + .IncrementsBy(10); + + modelBuilder.Entity("HomeWork21.Data.Entities.BreedEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("BreedName") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("Breed_name"); + + b.Property("CategoryId") + .HasColumnType("int") + .HasColumnName("Category_Id"); + + b.HasKey("Id"); + + b.HasIndex("CategoryId"); + + b.ToTable("Breeds"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.CategoryEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("CategoryName") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("Category_Name"); + + b.HasKey("Id"); + + b.ToTable("Categories"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.LocationEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("LocationName") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("Location_Name"); + + b.HasKey("Id"); + + b.ToTable("Locations"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.PetEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("Age") + .HasColumnType("real"); + + b.Property("BreedId") + .HasColumnType("int") + .HasColumnName("Breed_Id"); + + b.Property("CategoryId") + .HasColumnType("int") + .HasColumnName("Category_Id"); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("ImageUrl") + .HasColumnType("nvarchar(max)") + .HasColumnName("Image_Url"); + + b.Property("LocationId") + .HasColumnType("int") + .HasColumnName("Location_Id"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("BreedId"); + + b.HasIndex("CategoryId"); + + b.HasIndex("LocationId"); + + b.ToTable("Pet", "dbo"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.BreedEntity", b => + { + b.HasOne("HomeWork21.Data.Entities.CategoryEntity", "Category") + .WithMany("Breed") + .HasForeignKey("CategoryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Category"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.PetEntity", b => + { + b.HasOne("HomeWork21.Data.Entities.BreedEntity", "Breed") + .WithMany("Pet") + .HasForeignKey("BreedId") + .IsRequired(); + + b.HasOne("HomeWork21.Data.Entities.CategoryEntity", "Category") + .WithMany("Pet") + .HasForeignKey("CategoryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("HomeWork21.Data.Entities.LocationEntity", "Location") + .WithMany("Pet") + .HasForeignKey("LocationId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Breed"); + + b.Navigation("Category"); + + b.Navigation("Location"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.BreedEntity", b => + { + b.Navigation("Pet"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.CategoryEntity", b => + { + b.Navigation("Breed"); + + b.Navigation("Pet"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.LocationEntity", b => + { + b.Navigation("Pet"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/HomeWork21/HomeWork21/Migrations/20240321040818_ColumName.cs b/HomeWork21/HomeWork21/Migrations/20240321040818_ColumName.cs new file mode 100644 index 0000000..46bc0ae --- /dev/null +++ b/HomeWork21/HomeWork21/Migrations/20240321040818_ColumName.cs @@ -0,0 +1,226 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace HomeWork21.Migrations +{ + /// + public partial class ColumName : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Breeds_Categories_CategoryId", + table: "Breeds"); + + migrationBuilder.DropForeignKey( + name: "FK_Pet_Breeds_BreedId", + schema: "dbo", + table: "Pet"); + + migrationBuilder.DropForeignKey( + name: "FK_Pet_Categories_CategoryId", + schema: "dbo", + table: "Pet"); + + migrationBuilder.DropForeignKey( + name: "FK_Pet_Locations_LocationId", + schema: "dbo", + table: "Pet"); + + migrationBuilder.RenameColumn( + name: "LocationId", + schema: "dbo", + table: "Pet", + newName: "Location_Id"); + + migrationBuilder.RenameColumn( + name: "CategoryId", + schema: "dbo", + table: "Pet", + newName: "Category_Id"); + + migrationBuilder.RenameColumn( + name: "BreedId", + schema: "dbo", + table: "Pet", + newName: "Breed_Id"); + + migrationBuilder.RenameIndex( + name: "IX_Pet_LocationId", + schema: "dbo", + table: "Pet", + newName: "IX_Pet_Location_Id"); + + migrationBuilder.RenameIndex( + name: "IX_Pet_CategoryId", + schema: "dbo", + table: "Pet", + newName: "IX_Pet_Category_Id"); + + migrationBuilder.RenameIndex( + name: "IX_Pet_BreedId", + schema: "dbo", + table: "Pet", + newName: "IX_Pet_Breed_Id"); + + migrationBuilder.RenameColumn( + name: "breed_name", + table: "Breeds", + newName: "Breed_name"); + + migrationBuilder.RenameColumn( + name: "CategoryId", + table: "Breeds", + newName: "Category_Id"); + + migrationBuilder.RenameIndex( + name: "IX_Breeds_CategoryId", + table: "Breeds", + newName: "IX_Breeds_Category_Id"); + + migrationBuilder.AddForeignKey( + name: "FK_Breeds_Categories_Category_Id", + table: "Breeds", + column: "Category_Id", + principalTable: "Categories", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Pet_Breeds_Breed_Id", + schema: "dbo", + table: "Pet", + column: "Breed_Id", + principalTable: "Breeds", + principalColumn: "Id"); + + migrationBuilder.AddForeignKey( + name: "FK_Pet_Categories_Category_Id", + schema: "dbo", + table: "Pet", + column: "Category_Id", + principalTable: "Categories", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Pet_Locations_Location_Id", + schema: "dbo", + table: "Pet", + column: "Location_Id", + principalTable: "Locations", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Breeds_Categories_Category_Id", + table: "Breeds"); + + migrationBuilder.DropForeignKey( + name: "FK_Pet_Breeds_Breed_Id", + schema: "dbo", + table: "Pet"); + + migrationBuilder.DropForeignKey( + name: "FK_Pet_Categories_Category_Id", + schema: "dbo", + table: "Pet"); + + migrationBuilder.DropForeignKey( + name: "FK_Pet_Locations_Location_Id", + schema: "dbo", + table: "Pet"); + + migrationBuilder.RenameColumn( + name: "Location_Id", + schema: "dbo", + table: "Pet", + newName: "LocationId"); + + migrationBuilder.RenameColumn( + name: "Category_Id", + schema: "dbo", + table: "Pet", + newName: "CategoryId"); + + migrationBuilder.RenameColumn( + name: "Breed_Id", + schema: "dbo", + table: "Pet", + newName: "BreedId"); + + migrationBuilder.RenameIndex( + name: "IX_Pet_Location_Id", + schema: "dbo", + table: "Pet", + newName: "IX_Pet_LocationId"); + + migrationBuilder.RenameIndex( + name: "IX_Pet_Category_Id", + schema: "dbo", + table: "Pet", + newName: "IX_Pet_CategoryId"); + + migrationBuilder.RenameIndex( + name: "IX_Pet_Breed_Id", + schema: "dbo", + table: "Pet", + newName: "IX_Pet_BreedId"); + + migrationBuilder.RenameColumn( + name: "Breed_name", + table: "Breeds", + newName: "breed_name"); + + migrationBuilder.RenameColumn( + name: "Category_Id", + table: "Breeds", + newName: "CategoryId"); + + migrationBuilder.RenameIndex( + name: "IX_Breeds_Category_Id", + table: "Breeds", + newName: "IX_Breeds_CategoryId"); + + migrationBuilder.AddForeignKey( + name: "FK_Breeds_Categories_CategoryId", + table: "Breeds", + column: "CategoryId", + principalTable: "Categories", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Pet_Breeds_BreedId", + schema: "dbo", + table: "Pet", + column: "BreedId", + principalTable: "Breeds", + principalColumn: "Id"); + + migrationBuilder.AddForeignKey( + name: "FK_Pet_Categories_CategoryId", + schema: "dbo", + table: "Pet", + column: "CategoryId", + principalTable: "Categories", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Pet_Locations_LocationId", + schema: "dbo", + table: "Pet", + column: "LocationId", + principalTable: "Locations", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + } +} diff --git a/HomeWork21/HomeWork21/Migrations/20240321041029_ColumNameN.Designer.cs b/HomeWork21/HomeWork21/Migrations/20240321041029_ColumNameN.Designer.cs new file mode 100644 index 0000000..7c965ca --- /dev/null +++ b/HomeWork21/HomeWork21/Migrations/20240321041029_ColumNameN.Designer.cs @@ -0,0 +1,191 @@ +// +using HomeWork21.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace HomeWork21.Migrations +{ + [DbContext(typeof(AplicationDbContext))] + [Migration("20240321041029_ColumNameN")] + partial class ColumNameN + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseHiLo(modelBuilder, "EntityFrameworkHiLoSequence"); + + modelBuilder.HasSequence("EntityFrameworkHiLoSequence") + .IncrementsBy(10); + + modelBuilder.Entity("HomeWork21.Data.Entities.BreedEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("BreedName") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("Breed_Name"); + + b.Property("CategoryId") + .HasColumnType("int") + .HasColumnName("Category_Id"); + + b.HasKey("Id"); + + b.HasIndex("CategoryId"); + + b.ToTable("Breeds"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.CategoryEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("CategoryName") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("Category_Name"); + + b.HasKey("Id"); + + b.ToTable("Categories"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.LocationEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("LocationName") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("Location_Name"); + + b.HasKey("Id"); + + b.ToTable("Locations"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.PetEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("Age") + .HasColumnType("real"); + + b.Property("BreedId") + .HasColumnType("int") + .HasColumnName("Breed_Id"); + + b.Property("CategoryId") + .HasColumnType("int") + .HasColumnName("Category_Id"); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("ImageUrl") + .HasColumnType("nvarchar(max)") + .HasColumnName("Image_Url"); + + b.Property("LocationId") + .HasColumnType("int") + .HasColumnName("Location_Id"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("BreedId"); + + b.HasIndex("CategoryId"); + + b.HasIndex("LocationId"); + + b.ToTable("Pet", "dbo"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.BreedEntity", b => + { + b.HasOne("HomeWork21.Data.Entities.CategoryEntity", "Category") + .WithMany("Breed") + .HasForeignKey("CategoryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Category"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.PetEntity", b => + { + b.HasOne("HomeWork21.Data.Entities.BreedEntity", "Breed") + .WithMany("Pet") + .HasForeignKey("BreedId") + .IsRequired(); + + b.HasOne("HomeWork21.Data.Entities.CategoryEntity", "Category") + .WithMany("Pet") + .HasForeignKey("CategoryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("HomeWork21.Data.Entities.LocationEntity", "Location") + .WithMany("Pet") + .HasForeignKey("LocationId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Breed"); + + b.Navigation("Category"); + + b.Navigation("Location"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.BreedEntity", b => + { + b.Navigation("Pet"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.CategoryEntity", b => + { + b.Navigation("Breed"); + + b.Navigation("Pet"); + }); + + modelBuilder.Entity("HomeWork21.Data.Entities.LocationEntity", b => + { + b.Navigation("Pet"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/HomeWork21/HomeWork21/Migrations/20240321041029_ColumNameN.cs b/HomeWork21/HomeWork21/Migrations/20240321041029_ColumNameN.cs new file mode 100644 index 0000000..5e9d328 --- /dev/null +++ b/HomeWork21/HomeWork21/Migrations/20240321041029_ColumNameN.cs @@ -0,0 +1,28 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace HomeWork21.Migrations +{ + /// + public partial class ColumNameN : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.RenameColumn( + name: "Breed_name", + table: "Breeds", + newName: "Breed_Name"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.RenameColumn( + name: "Breed_Name", + table: "Breeds", + newName: "Breed_name"); + } + } +} diff --git a/HomeWork21/HomeWork21/Migrations/AplicationDbContextModelSnapshot.cs b/HomeWork21/HomeWork21/Migrations/AplicationDbContextModelSnapshot.cs index 8b24718..1f5e203 100644 --- a/HomeWork21/HomeWork21/Migrations/AplicationDbContextModelSnapshot.cs +++ b/HomeWork21/HomeWork21/Migrations/AplicationDbContextModelSnapshot.cs @@ -35,10 +35,11 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("BreedName") .IsRequired() .HasColumnType("nvarchar(max)") - .HasColumnName("breed_name"); + .HasColumnName("Breed_Name"); b.Property("CategoryId") - .HasColumnType("int"); + .HasColumnType("int") + .HasColumnName("Category_Id"); b.HasKey("Id"); @@ -95,10 +96,12 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasColumnType("real"); b.Property("BreedId") - .HasColumnType("int"); + .HasColumnType("int") + .HasColumnName("Breed_Id"); b.Property("CategoryId") - .HasColumnType("int"); + .HasColumnType("int") + .HasColumnName("Category_Id"); b.Property("Description") .HasColumnType("nvarchar(max)"); @@ -108,7 +111,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasColumnName("Image_Url"); b.Property("LocationId") - .HasColumnType("int"); + .HasColumnType("int") + .HasColumnName("Location_Id"); b.Property("Name") .IsRequired() @@ -122,7 +126,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasIndex("LocationId"); - b.ToTable("Pets"); + b.ToTable("Pet", "dbo"); }); modelBuilder.Entity("HomeWork21.Data.Entities.BreedEntity", b => @@ -130,6 +134,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasOne("HomeWork21.Data.Entities.CategoryEntity", "Category") .WithMany("Breed") .HasForeignKey("CategoryId") + .OnDelete(DeleteBehavior.Cascade) .IsRequired(); b.Navigation("Category"); @@ -145,6 +150,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasOne("HomeWork21.Data.Entities.CategoryEntity", "Category") .WithMany("Pet") .HasForeignKey("CategoryId") + .OnDelete(DeleteBehavior.Cascade) .IsRequired(); b.HasOne("HomeWork21.Data.Entities.LocationEntity", "Location") diff --git a/HomeWork21/HomeWork21/Models/Breed.cs b/HomeWork21/HomeWork21/Models/Breed.cs index 6f1c564..b98a913 100644 --- a/HomeWork21/HomeWork21/Models/Breed.cs +++ b/HomeWork21/HomeWork21/Models/Breed.cs @@ -4,6 +4,6 @@ internal class Breed { public int Id { get; set; } public string Name { get; set; } = null!; - public int CategoryId { get; set; } + public Category? Category { get; set; } } }