Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions HomeWork21/HomeWork21.sln
Original file line number Diff line number Diff line change
@@ -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
62 changes: 62 additions & 0 deletions HomeWork21/HomeWork21/App.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using HomeWork21.Data;
using HomeWork21.Models;
using Microsoft.EntityFrameworkCore;

namespace HomeWork21
{
internal class App
{
private readonly AplicationDbContext _dbContext;

public App(AplicationDbContext dbContext)
{
_dbContext = dbContext;
}

public async Task StartUpAsync()
{

List<Pet> pets = new List<Pet>();

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
}

});
}
}
}
}
27 changes: 27 additions & 0 deletions HomeWork21/HomeWork21/Data/AplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -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<AplicationDbContext> options)
: base(options)
{ }

public DbSet<BreedEntity> Breeds { get; set; } = null!;
public DbSet<CategoryEntity> Categories { get; set; } = null!;
public DbSet<LocationEntity> Locations { get; set; } = null!;
public DbSet<PetEntity> 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();
}
}
}
29 changes: 29 additions & 0 deletions HomeWork21/HomeWork21/Data/AplicationDbContextFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;

namespace HomeWork21.Data
{
internal class AplicationDbContextFactory : IDesignTimeDbContextFactory<AplicationDbContext>
{
public AplicationDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<AplicationDbContext>();

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);
}
}
}
12 changes: 12 additions & 0 deletions HomeWork21/HomeWork21/Data/Entities/BreedEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace HomeWork21.Data.Entities
{
internal class BreedEntity
{
public int Id { get; set; }
public string BreedName { get; set; } = null!;
public ICollection<PetEntity> Pet { get; set; } = new List<PetEntity>();

public int CategoryId { get; set; }
public CategoryEntity? Category { get; set; }
}
}
11 changes: 11 additions & 0 deletions HomeWork21/HomeWork21/Data/Entities/CategoryEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace HomeWork21.Data.Entities
{
internal class CategoryEntity
{
public int Id { get; set; }
public string CategoryName { get; set; } = null!;

public ICollection<PetEntity> Pet { get; set; } = new List<PetEntity>();
public ICollection<BreedEntity> Breed { get; set; } = new List<BreedEntity>();
}
}
10 changes: 10 additions & 0 deletions HomeWork21/HomeWork21/Data/Entities/LocationEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace HomeWork21.Data.Entities
{
internal class LocationEntity
{
public int Id { get; set; }
public string LocationName { get; set; } = null!;

public ICollection<PetEntity> Pet { get; set; } = new List<PetEntity>();
}
}
18 changes: 18 additions & 0 deletions HomeWork21/HomeWork21/Data/Entities/PetEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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 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; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using HomeWork21.Data.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace HomeWork21.Data.EntitiesConfigure
{
internal class BreedEntityConfigure : IEntityTypeConfiguration<BreedEntity>
{
public void Configure(EntityTypeBuilder<BreedEntity> builder)
{
builder.HasKey(x => x.Id);

builder
.Property(p => p.BreedName)
.IsRequired()
.HasColumnName("Breed_Name");

builder
.Property(p => p.CategoryId)
.IsRequired()
.HasColumnName("Category_Id");

builder
.HasOne(o => o.Category)
.WithMany(m => m.Breed)
.HasForeignKey(o => o.CategoryId)
.OnDelete(DeleteBehavior.Cascade);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using HomeWork21.Data.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace HomeWork21.Data.EntitiesConfigure
{
internal class CategoryEntityConfigure : IEntityTypeConfiguration<CategoryEntity>
{
public void Configure(EntityTypeBuilder<CategoryEntity> builder)
{
builder.HasKey(k => k.Id);
builder
.Property(p => p.CategoryName)
.IsRequired()
.HasColumnName("Category_Name");

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using HomeWork21.Data.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace HomeWork21.Data.EntitiesConfigure
{
internal class LocationEntityConfigure : IEntityTypeConfiguration<LocationEntity>
{
public void Configure(EntityTypeBuilder<LocationEntity> builder)
{
builder.HasKey(k => k.Id);

builder
.Property(p => p.LocationName)
.IsRequired()
.HasColumnName("Location_Name");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using HomeWork21.Data.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace HomeWork21.Data.EntitiesConfigure
{
internal class PetEntityConfiguration : IEntityTypeConfiguration<PetEntity>
{
public void Configure(EntityTypeBuilder<PetEntity> 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().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)
.WithMany(m => m.Pet)
.HasForeignKey(o => o.CategoryId)
.OnDelete(DeleteBehavior.Cascade);

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);
}
}
}
37 changes: 37 additions & 0 deletions HomeWork21/HomeWork21/HomeWork21.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Content Include="bin\Debug\net8.0\config.json" />
<Content Include="config.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
</ItemGroup>

</Project>
Loading