diff --git a/Address Directory/Address Directory.sln b/Address Directory/Address Directory.sln index 41b87ae..8ad6768 100644 --- a/Address Directory/Address Directory.sln +++ b/Address Directory/Address Directory.sln @@ -16,9 +16,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Contracts", "Contracts\Cont EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Services", "Services\Services.csproj", "{4CCFADCD-9E80-4B66-A6D8-B6FC5490A0C0}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Presentation", "Presentation\Presentation.csproj", "{9359CCCA-F725-462F-AACD-8FCAE5C907E5}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Presentation", "Presentation\Presentation.csproj", "{9359CCCA-F725-462F-AACD-8FCAE5C907E5}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Web", "Web\Web.csproj", "{EB3EE34B-8C85-4EC8-9E78-CFBECED2E0AD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Web", "Web\Web.csproj", "{EB3EE34B-8C85-4EC8-9E78-CFBECED2E0AD}" ProjectSection(ProjectDependencies) = postProject {9359CCCA-F725-462F-AACD-8FCAE5C907E5} = {9359CCCA-F725-462F-AACD-8FCAE5C907E5} EndProjectSection diff --git a/Address Directory/Persistence/Migrations/20241210151814_mig2.Designer.cs b/Address Directory/Persistence/Migrations/20241210215030_mi.Designer.cs similarity index 91% rename from Address Directory/Persistence/Migrations/20241210151814_mig2.Designer.cs rename to Address Directory/Persistence/Migrations/20241210215030_mi.Designer.cs index b97fbb8..73ed81d 100644 --- a/Address Directory/Persistence/Migrations/20241210151814_mig2.Designer.cs +++ b/Address Directory/Persistence/Migrations/20241210215030_mi.Designer.cs @@ -13,8 +13,8 @@ using Persistence; namespace Persistence.Migrations { [DbContext(typeof(RepositoryDbContext))] - [Migration("20241210151814_mig2")] - partial class mig2 + [Migration("20241210215030_mi")] + partial class mi { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -34,7 +34,8 @@ namespace Persistence.Migrations b.Property("Name") .IsRequired() - .HasColumnType("text"); + .HasMaxLength(100) + .HasColumnType("character varying(100)"); b.Property("RegionId") .HasColumnType("uuid"); @@ -57,7 +58,8 @@ namespace Persistence.Migrations b.Property("Name") .IsRequired() - .HasColumnType("text"); + .HasMaxLength(100) + .HasColumnType("character varying(100)"); b.HasKey("Id"); @@ -82,7 +84,8 @@ namespace Persistence.Migrations b.Property("Name") .IsRequired() - .HasColumnType("text"); + .HasMaxLength(100) + .HasColumnType("character varying(100)"); b.Property("OkatoCode") .HasColumnType("integer"); diff --git a/Address Directory/Persistence/Migrations/20241210151814_mig2.cs b/Address Directory/Persistence/Migrations/20241210215030_mi.cs similarity index 89% rename from Address Directory/Persistence/Migrations/20241210151814_mig2.cs rename to Address Directory/Persistence/Migrations/20241210215030_mi.cs index 72207a8..9313285 100644 --- a/Address Directory/Persistence/Migrations/20241210151814_mig2.cs +++ b/Address Directory/Persistence/Migrations/20241210215030_mi.cs @@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore.Migrations; namespace Persistence.Migrations { /// - public partial class mig2 : Migration + public partial class mi : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -17,7 +17,7 @@ namespace Persistence.Migrations columns: table => new { Id = table.Column(type: "uuid", nullable: false), - Name = table.Column(type: "text", nullable: false), + Name = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), Code = table.Column(type: "integer", nullable: false) }, constraints: table => @@ -30,7 +30,7 @@ namespace Persistence.Migrations columns: table => new { Id = table.Column(type: "uuid", nullable: false), - Name = table.Column(type: "text", nullable: false), + Name = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), RegionId = table.Column(type: "uuid", nullable: false) }, constraints: table => @@ -49,7 +49,7 @@ namespace Persistence.Migrations columns: table => new { Id = table.Column(type: "uuid", nullable: false), - Name = table.Column(type: "text", nullable: false), + Name = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), HouseNumbers = table.Column>(type: "integer[]", nullable: false), Index = table.Column(type: "integer", nullable: false), OkatoCode = table.Column(type: "integer", nullable: false), diff --git a/Address Directory/Persistence/Migrations/RepositoryDbContextModelSnapshot.cs b/Address Directory/Persistence/Migrations/RepositoryDbContextModelSnapshot.cs index d27f773..779a82d 100644 --- a/Address Directory/Persistence/Migrations/RepositoryDbContextModelSnapshot.cs +++ b/Address Directory/Persistence/Migrations/RepositoryDbContextModelSnapshot.cs @@ -31,7 +31,8 @@ namespace Persistence.Migrations b.Property("Name") .IsRequired() - .HasColumnType("text"); + .HasMaxLength(100) + .HasColumnType("character varying(100)"); b.Property("RegionId") .HasColumnType("uuid"); @@ -54,7 +55,8 @@ namespace Persistence.Migrations b.Property("Name") .IsRequired() - .HasColumnType("text"); + .HasMaxLength(100) + .HasColumnType("character varying(100)"); b.HasKey("Id"); @@ -79,7 +81,8 @@ namespace Persistence.Migrations b.Property("Name") .IsRequired() - .HasColumnType("text"); + .HasMaxLength(100) + .HasColumnType("character varying(100)"); b.Property("OkatoCode") .HasColumnType("integer"); diff --git a/Address Directory/Persistence/Persistence.csproj b/Address Directory/Persistence/Persistence.csproj index e35ee51..13653be 100644 --- a/Address Directory/Persistence/Persistence.csproj +++ b/Address Directory/Persistence/Persistence.csproj @@ -13,7 +13,9 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - + + + diff --git a/Address Directory/Persistence/RepositoryDbContext.cs b/Address Directory/Persistence/RepositoryDbContext.cs index 6fc6740..b7826bd 100644 --- a/Address Directory/Persistence/RepositoryDbContext.cs +++ b/Address Directory/Persistence/RepositoryDbContext.cs @@ -13,5 +13,9 @@ namespace Persistence : base(options) { } + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.ApplyConfigurationsFromAssembly(typeof(RepositoryDbContext).Assembly); + } } } diff --git a/Address Directory/Persistence/RepositoryDbContextFactory.cs b/Address Directory/Persistence/RepositoryDbContextFactory.cs new file mode 100644 index 0000000..9da4015 --- /dev/null +++ b/Address Directory/Persistence/RepositoryDbContextFactory.cs @@ -0,0 +1,19 @@ +using Microsoft.EntityFrameworkCore.Design; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Persistence; + +public class RepositoryDbContextFactory : IDesignTimeDbContextFactory +{ + public RepositoryDbContext CreateDbContext(string[] args) + { + var configuration = new ConfigurationBuilder() + .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../Web")) + .AddJsonFile("appsettings.json") + .Build(); + var connectionString = configuration.GetConnectionString("Database"); + var optionsBuilder = new DbContextOptionsBuilder(); + optionsBuilder.UseNpgsql(connectionString); + return new RepositoryDbContext(optionsBuilder.Options); + } +} diff --git a/Address Directory/Presentation/Presentation.csproj b/Address Directory/Presentation/Presentation.csproj index 9a1cf52..a98fa9c 100644 --- a/Address Directory/Presentation/Presentation.csproj +++ b/Address Directory/Presentation/Presentation.csproj @@ -7,7 +7,7 @@ - + diff --git a/Address Directory/Web/Program.cs b/Address Directory/Web/Program.cs index a3e59a3..8f603f2 100644 --- a/Address Directory/Web/Program.cs +++ b/Address Directory/Web/Program.cs @@ -9,16 +9,13 @@ using Services.Profiles; var builder = WebApplication.CreateBuilder(args); var configuration = builder.Configuration; +var connectionString = builder.Configuration.GetConnectionString("Database"); + builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -builder.Services.AddDbContext( - options => - { - options.UseNpgsql(configuration.GetConnectionString(nameof(RepositoryDbContext))); - }); builder.Services.AddScoped(); builder.Services.AddScoped(); @@ -28,8 +25,15 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddDbContext( + options => + { + options.UseNpgsql(connectionString); + }); + builder.Services.AddAutoMapper(typeof(MappingProfile)); var app = builder.Build(); + if (app.Environment.IsDevelopment()) { app.UseSwagger(); diff --git a/Address Directory/Web/Web.csproj b/Address Directory/Web/Web.csproj index d466a39..3b4a438 100644 --- a/Address Directory/Web/Web.csproj +++ b/Address Directory/Web/Web.csproj @@ -11,7 +11,8 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + + diff --git a/Address Directory/Web/appsettings.Development.json b/Address Directory/Web/appsettings.Development.json index 7acedde..770d3e9 100644 --- a/Address Directory/Web/appsettings.Development.json +++ b/Address Directory/Web/appsettings.Development.json @@ -5,8 +5,5 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } - }, - "ConnectionStrings": { - "RepositoryDbContext":"Host=localhost;Port=5432;Database=AdressDb;Username=postgres;Password=postgres" } } diff --git a/Address Directory/Web/appsettings.json b/Address Directory/Web/appsettings.json index 10f68b8..5389665 100644 --- a/Address Directory/Web/appsettings.json +++ b/Address Directory/Web/appsettings.json @@ -5,5 +5,8 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "ConnectionStrings": { + "Database": "Host=localhost;Port=5432;Database=AdressDb;Username=postgres;Password=postgres" + } }