Прблема с БД

This commit is contained in:
romai 2024-12-10 19:20:51 +04:00
parent 0e886547ae
commit 796faede69
9 changed files with 29 additions and 198 deletions

View File

@ -1,137 +0,0 @@
// <auto-generated />
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Persistence;
#nullable disable
namespace Persistence.Migrations
{
[DbContext(typeof(RepositoryDbContext))]
[Migration("20241208232209_mig1")]
partial class mig1
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.0")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("Domain.Entities.City", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");
b.Property<Guid>("RegionId")
.HasColumnType("uuid");
b.HasKey("Id");
b.HasIndex("RegionId");
b.ToTable("Cities");
});
modelBuilder.Entity("Domain.Entities.Region", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<int>("Code")
.HasColumnType("integer");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");
b.HasKey("Id");
b.ToTable("Regions");
});
modelBuilder.Entity("Domain.Entities.Street", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<Guid>("CityId")
.HasColumnType("uuid");
b.PrimitiveCollection<List<int>>("HouseNumbers")
.IsRequired()
.HasColumnType("integer[]");
b.Property<int>("Index")
.HasColumnType("integer");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");
b.Property<int>("OkatoCode")
.HasColumnType("integer");
b.Property<int>("TaxCode")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("CityId");
b.ToTable("Streets");
});
modelBuilder.Entity("Domain.Entities.City", b =>
{
b.HasOne("Domain.Entities.Region", "Region")
.WithMany("Cities")
.HasForeignKey("RegionId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Region");
});
modelBuilder.Entity("Domain.Entities.Street", b =>
{
b.HasOne("Domain.Entities.City", "City")
.WithMany("Streets")
.HasForeignKey("CityId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("City");
});
modelBuilder.Entity("Domain.Entities.City", b =>
{
b.Navigation("Streets");
});
modelBuilder.Entity("Domain.Entities.Region", b =>
{
b.Navigation("Cities");
});
#pragma warning restore 612, 618
}
}
}

View File

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

View File

@ -13,7 +13,7 @@ using Persistence;
namespace Persistence.Migrations
{
[DbContext(typeof(RepositoryDbContext))]
[Migration("20241208232448_mig2")]
[Migration("20241210151814_mig2")]
partial class mig2
{
/// <inheritdoc />
@ -34,8 +34,7 @@ namespace Persistence.Migrations
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");
.HasColumnType("text");
b.Property<Guid>("RegionId")
.HasColumnType("uuid");
@ -58,8 +57,7 @@ namespace Persistence.Migrations
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");
.HasColumnType("text");
b.HasKey("Id");
@ -84,8 +82,7 @@ namespace Persistence.Migrations
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");
.HasColumnType("text");
b.Property<int>("OkatoCode")
.HasColumnType("integer");

View File

@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Persistence.Migrations
{
/// <inheritdoc />
public partial class mig1 : Migration
public partial class mig2 : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
@ -17,7 +17,7 @@ namespace Persistence.Migrations
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
Name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
Name = table.Column<string>(type: "text", nullable: false),
Code = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
@ -30,7 +30,7 @@ namespace Persistence.Migrations
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
Name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
Name = table.Column<string>(type: "text", nullable: false),
RegionId = table.Column<Guid>(type: "uuid", nullable: false)
},
constraints: table =>
@ -49,7 +49,7 @@ namespace Persistence.Migrations
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
Name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
Name = table.Column<string>(type: "text", nullable: false),
HouseNumbers = table.Column<List<int>>(type: "integer[]", nullable: false),
Index = table.Column<int>(type: "integer", nullable: false),
OkatoCode = table.Column<int>(type: "integer", nullable: false),

View File

@ -31,8 +31,7 @@ namespace Persistence.Migrations
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");
.HasColumnType("text");
b.Property<Guid>("RegionId")
.HasColumnType("uuid");
@ -55,8 +54,7 @@ namespace Persistence.Migrations
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");
.HasColumnType("text");
b.HasKey("Id");
@ -81,8 +79,7 @@ namespace Persistence.Migrations
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");
.HasColumnType("text");
b.Property<int>("OkatoCode")
.HasColumnType("integer");

View File

@ -5,27 +5,13 @@ namespace Persistence
{
public class RepositoryDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseNpgsql(@"
Host=localhost;
Port=5432;
Database=AdressDirectory;
Username=postgres;
Password=postgres");
}
base.OnConfiguring(optionsBuilder);
}
public DbSet<Region> Regions { get; set; }
public DbSet<City> Cities { get; set; }
public DbSet<Street> Streets { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
public RepositoryDbContext(DbContextOptions<RepositoryDbContext> options)
: base(options)
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(RepositoryDbContext).Assembly);
}
}
}

View File

@ -7,15 +7,19 @@ using Services.Abstractions;
using Services.Profiles;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("Database");
var configuration = builder.Configuration;
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContextPool<RepositoryDbContext>(options =>
{
options.UseNpgsql(connectionString);
});
builder.Services.AddDbContext<RepositoryDbContext>(
options =>
{
options.UseNpgsql(configuration.GetConnectionString(nameof(RepositoryDbContext)));
});
builder.Services.AddScoped<IRegionService, RegionService>();
builder.Services.AddScoped<ICityService, CityService>();
builder.Services.AddScoped<IStreetService, StreetService>();

View File

@ -13,6 +13,7 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5044",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
@ -21,6 +22,7 @@
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchUrl": "swagger",
"launchBrowser": true,
"applicationUrl": "https://localhost:7184;http://localhost:5044",
"environmentVariables": {
@ -30,6 +32,7 @@
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}

View File

@ -5,5 +5,8 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"RepositoryDbContext":"Host=localhost;Port=5432;Database=AdressDb;Username=postgres;Password=postgres"
}
}