diff --git a/Persistence.API/Persistence.API.csproj b/Persistence.API/Persistence.API.csproj index f71c59d..40bed8a 100644 --- a/Persistence.API/Persistence.API.csproj +++ b/Persistence.API/Persistence.API.csproj @@ -13,6 +13,7 @@ + diff --git a/Persistence.API/Program.cs b/Persistence.API/Program.cs index 51f3e13..93e62a1 100644 --- a/Persistence.API/Program.cs +++ b/Persistence.API/Program.cs @@ -1,11 +1,4 @@ -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Persistence.Repositories; -using Persistence.Repository; -using Persistence.Repository.Data; -using Persistence.Repository.Repositories; - namespace Persistence.API; public class Program @@ -13,7 +6,7 @@ public class Program public static void Main(string[] args) { var host = CreateHostBuilder(args).Build(); - Persistence.Repository.Startup.BeforeRunHandler(host); + Startup.BeforeRunHandler(host); host.Run(); } diff --git a/Persistence.API/Startup.cs b/Persistence.API/Startup.cs index d559627..1afaa18 100644 --- a/Persistence.API/Startup.cs +++ b/Persistence.API/Startup.cs @@ -1,4 +1,6 @@ using Persistence.Repository; +using Persistence.Database.Model; +using Persistence.Database.Postgres; namespace Persistence.API; @@ -18,8 +20,8 @@ public class Startup // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle services.AddEndpointsApiExplorer(); services.AddSwaggerGen(); - - services.AddInfrastructure(Configuration); + services.AddPersistenceDbContext(Configuration); + services.AddInfrastructure(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) @@ -37,4 +39,15 @@ public class Startup endpoints.MapControllers(); }); } + + public static void BeforeRunHandler(IHost host) + { + using var scope = host.Services.CreateScope(); + var provider = scope.ServiceProvider; + + var context = provider.GetRequiredService(); + context.Database.EnsureCreatedAndMigrated(); + } + + } diff --git a/Persistence.Database.Postgres/DependencyInjection.cs b/Persistence.Database.Postgres/DependencyInjection.cs new file mode 100644 index 0000000..1498932 --- /dev/null +++ b/Persistence.Database.Postgres/DependencyInjection.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Persistence.Database; +using Persistence.Database.Model; + +namespace Persistence.Database.Model; + +public static class DependencyInjection +{ + public static IServiceCollection AddPersistenceDbContext(this IServiceCollection services, IConfiguration configuration) + { + string connectionStringName = "DefaultConnection"; + + services.AddDbContext(options => + options.UseNpgsql(configuration.GetConnectionString(connectionStringName))); + + services.AddScoped(provider => provider.GetRequiredService()); + + return services; + } +} diff --git a/Persistence.Database/EFExtensionsInitialization.cs b/Persistence.Database.Postgres/EFExtensionsInitialization.cs similarity index 98% rename from Persistence.Database/EFExtensionsInitialization.cs rename to Persistence.Database.Postgres/EFExtensionsInitialization.cs index 3b28f61..3d2752f 100644 --- a/Persistence.Database/EFExtensionsInitialization.cs +++ b/Persistence.Database.Postgres/EFExtensionsInitialization.cs @@ -7,7 +7,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Persistence.Database; +namespace Persistence.Database.Postgres; public static class EFExtensionsInitialization { public static void EnsureCreatedAndMigrated(this DatabaseFacade db) diff --git a/Persistence.Database.Postgres/Migrations/20241115105149_InitialCreate.Designer.cs b/Persistence.Database.Postgres/Migrations/20241115105149_InitialCreate.Designer.cs new file mode 100644 index 0000000..b66203a --- /dev/null +++ b/Persistence.Database.Postgres/Migrations/20241115105149_InitialCreate.Designer.cs @@ -0,0 +1,122 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using Persistence.Database.Model; + +#nullable disable + +namespace Persistence.Database.Postgres.Migrations +{ + [DbContext(typeof(PersistenceDbContext))] + [Migration("20241115105149_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseCollation("Russian_Russia.1251") + .HasAnnotation("ProductVersion", "8.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.HasPostgresExtension(modelBuilder, "adminpack"); + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Persistence.Database.Model.DataSaub", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("AxialLoad") + .HasColumnType("double precision") + .HasColumnName("axialLoad"); + + b.Property("BitDepth") + .HasColumnType("double precision") + .HasColumnName("bitDepth"); + + b.Property("BlockPosition") + .HasColumnType("double precision") + .HasColumnName("blockPosition"); + + b.Property("BlockSpeed") + .HasColumnType("double precision") + .HasColumnName("blockSpeed"); + + b.Property("Flow") + .HasColumnType("double precision") + .HasColumnName("flow"); + + b.Property("HookWeight") + .HasColumnType("double precision") + .HasColumnName("hookWeight"); + + b.Property("IdFeedRegulator") + .HasColumnType("integer") + .HasColumnName("idFeedRegulator"); + + b.Property("Mode") + .HasColumnType("integer") + .HasColumnName("mode"); + + b.Property("Mse") + .HasColumnType("double precision") + .HasColumnName("mse"); + + b.Property("MseState") + .HasColumnType("smallint") + .HasColumnName("mseState"); + + b.Property("Pressure") + .HasColumnType("double precision") + .HasColumnName("pressure"); + + b.Property("Pump0Flow") + .HasColumnType("double precision") + .HasColumnName("pump0Flow"); + + b.Property("Pump1Flow") + .HasColumnType("double precision") + .HasColumnName("pump1Flow"); + + b.Property("Pump2Flow") + .HasColumnType("double precision") + .HasColumnName("pump2Flow"); + + b.Property("RotorSpeed") + .HasColumnType("double precision") + .HasColumnName("rotorSpeed"); + + b.Property("RotorTorque") + .HasColumnType("double precision") + .HasColumnName("rotorTorque"); + + b.Property("TimeStamp") + .HasColumnType("integer") + .HasColumnName("timestamp"); + + b.Property("User") + .HasColumnType("text") + .HasColumnName("user"); + + b.Property("WellDepth") + .HasColumnType("double precision") + .HasColumnName("wellDepth"); + + b.HasKey("Id"); + + b.ToTable("DataSaub"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Persistence.Database.Postgres/Migrations/20241115105149_InitialCreate.cs b/Persistence.Database.Postgres/Migrations/20241115105149_InitialCreate.cs new file mode 100644 index 0000000..69839cd --- /dev/null +++ b/Persistence.Database.Postgres/Migrations/20241115105149_InitialCreate.cs @@ -0,0 +1,56 @@ +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Persistence.Database.Postgres.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterDatabase() + .Annotation("Npgsql:PostgresExtension:adminpack", ",,"); + + migrationBuilder.CreateTable( + name: "DataSaub", + columns: table => new + { + id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + timestamp = table.Column(type: "integer", nullable: false), + mode = table.Column(type: "integer", nullable: true), + user = table.Column(type: "text", nullable: true), + wellDepth = table.Column(type: "double precision", nullable: true), + bitDepth = table.Column(type: "double precision", nullable: true), + blockPosition = table.Column(type: "double precision", nullable: true), + blockSpeed = table.Column(type: "double precision", nullable: true), + pressure = table.Column(type: "double precision", nullable: true), + axialLoad = table.Column(type: "double precision", nullable: true), + hookWeight = table.Column(type: "double precision", nullable: true), + rotorTorque = table.Column(type: "double precision", nullable: true), + rotorSpeed = table.Column(type: "double precision", nullable: true), + flow = table.Column(type: "double precision", nullable: true), + mseState = table.Column(type: "smallint", nullable: false), + idFeedRegulator = table.Column(type: "integer", nullable: false), + mse = table.Column(type: "double precision", nullable: true), + pump0Flow = table.Column(type: "double precision", nullable: true), + pump1Flow = table.Column(type: "double precision", nullable: true), + pump2Flow = table.Column(type: "double precision", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_DataSaub", x => x.id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "DataSaub"); + } + } +} diff --git a/Persistence.Database.Postgres/Migrations/20241118052225_SetpointMigration.Designer.cs b/Persistence.Database.Postgres/Migrations/20241118052225_SetpointMigration.Designer.cs new file mode 100644 index 0000000..9399dd4 --- /dev/null +++ b/Persistence.Database.Postgres/Migrations/20241118052225_SetpointMigration.Designer.cs @@ -0,0 +1,146 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using Persistence.Database.Model; + +#nullable disable + +namespace Persistence.Database.Postgres.Migrations +{ + [DbContext(typeof(PersistenceDbContext))] + [Migration("20241118052225_SetpointMigration")] + partial class SetpointMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseCollation("Russian_Russia.1251") + .HasAnnotation("ProductVersion", "8.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.HasPostgresExtension(modelBuilder, "adminpack"); + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Persistence.Database.Model.DataSaub", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("AxialLoad") + .HasColumnType("double precision") + .HasColumnName("axialLoad"); + + b.Property("BitDepth") + .HasColumnType("double precision") + .HasColumnName("bitDepth"); + + b.Property("BlockPosition") + .HasColumnType("double precision") + .HasColumnName("blockPosition"); + + b.Property("BlockSpeed") + .HasColumnType("double precision") + .HasColumnName("blockSpeed"); + + b.Property("Flow") + .HasColumnType("double precision") + .HasColumnName("flow"); + + b.Property("HookWeight") + .HasColumnType("double precision") + .HasColumnName("hookWeight"); + + b.Property("IdFeedRegulator") + .HasColumnType("integer") + .HasColumnName("idFeedRegulator"); + + b.Property("Mode") + .HasColumnType("integer") + .HasColumnName("mode"); + + b.Property("Mse") + .HasColumnType("double precision") + .HasColumnName("mse"); + + b.Property("MseState") + .HasColumnType("smallint") + .HasColumnName("mseState"); + + b.Property("Pressure") + .HasColumnType("double precision") + .HasColumnName("pressure"); + + b.Property("Pump0Flow") + .HasColumnType("double precision") + .HasColumnName("pump0Flow"); + + b.Property("Pump1Flow") + .HasColumnType("double precision") + .HasColumnName("pump1Flow"); + + b.Property("Pump2Flow") + .HasColumnType("double precision") + .HasColumnName("pump2Flow"); + + b.Property("RotorSpeed") + .HasColumnType("double precision") + .HasColumnName("rotorSpeed"); + + b.Property("RotorTorque") + .HasColumnType("double precision") + .HasColumnName("rotorTorque"); + + b.Property("TimeStamp") + .HasColumnType("integer") + .HasColumnName("timestamp"); + + b.Property("User") + .HasColumnType("text") + .HasColumnName("user"); + + b.Property("WellDepth") + .HasColumnType("double precision") + .HasColumnName("wellDepth"); + + b.HasKey("Id"); + + b.ToTable("DataSaub"); + }); + + modelBuilder.Entity("Persistence.Database.Model.Setpoint", b => + { + b.Property("Key") + .HasColumnType("uuid") + .HasComment("Ключ"); + + b.Property("Created") + .HasColumnType("timestamp with time zone") + .HasComment("Дата изменения уставки"); + + b.Property("IdUser") + .HasColumnType("integer") + .HasComment("Id автора последнего изменения"); + + b.Property("Value") + .IsRequired() + .HasColumnType("jsonb") + .HasComment("Значение уставки"); + + b.HasKey("Key", "Created"); + + b.ToTable("Setpoint"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Persistence.Database.Postgres/Migrations/20241118052225_SetpointMigration.cs b/Persistence.Database.Postgres/Migrations/20241118052225_SetpointMigration.cs new file mode 100644 index 0000000..49e438a --- /dev/null +++ b/Persistence.Database.Postgres/Migrations/20241118052225_SetpointMigration.cs @@ -0,0 +1,36 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Persistence.Database.Postgres.Migrations +{ + /// + public partial class SetpointMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Setpoint", + columns: table => new + { + Key = table.Column(type: "uuid", nullable: false, comment: "Ключ"), + Created = table.Column(type: "timestamp with time zone", nullable: false, comment: "Дата изменения уставки"), + Value = table.Column(type: "jsonb", nullable: false, comment: "Значение уставки"), + IdUser = table.Column(type: "integer", nullable: false, comment: "Id автора последнего изменения") + }, + constraints: table => + { + table.PrimaryKey("PK_Setpoint", x => new { x.Key, x.Created }); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Setpoint"); + } + } +} diff --git a/Persistence.Database.Postgres/Migrations/PersistenceDbContextModelSnapshot.cs b/Persistence.Database.Postgres/Migrations/PersistenceDbContextModelSnapshot.cs new file mode 100644 index 0000000..6c404fe --- /dev/null +++ b/Persistence.Database.Postgres/Migrations/PersistenceDbContextModelSnapshot.cs @@ -0,0 +1,143 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using Persistence.Database.Model; + +#nullable disable + +namespace Persistence.Database.Postgres.Migrations +{ + [DbContext(typeof(PersistenceDbContext))] + partial class PersistenceDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseCollation("Russian_Russia.1251") + .HasAnnotation("ProductVersion", "8.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.HasPostgresExtension(modelBuilder, "adminpack"); + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Persistence.Database.Model.DataSaub", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("AxialLoad") + .HasColumnType("double precision") + .HasColumnName("axialLoad"); + + b.Property("BitDepth") + .HasColumnType("double precision") + .HasColumnName("bitDepth"); + + b.Property("BlockPosition") + .HasColumnType("double precision") + .HasColumnName("blockPosition"); + + b.Property("BlockSpeed") + .HasColumnType("double precision") + .HasColumnName("blockSpeed"); + + b.Property("Flow") + .HasColumnType("double precision") + .HasColumnName("flow"); + + b.Property("HookWeight") + .HasColumnType("double precision") + .HasColumnName("hookWeight"); + + b.Property("IdFeedRegulator") + .HasColumnType("integer") + .HasColumnName("idFeedRegulator"); + + b.Property("Mode") + .HasColumnType("integer") + .HasColumnName("mode"); + + b.Property("Mse") + .HasColumnType("double precision") + .HasColumnName("mse"); + + b.Property("MseState") + .HasColumnType("smallint") + .HasColumnName("mseState"); + + b.Property("Pressure") + .HasColumnType("double precision") + .HasColumnName("pressure"); + + b.Property("Pump0Flow") + .HasColumnType("double precision") + .HasColumnName("pump0Flow"); + + b.Property("Pump1Flow") + .HasColumnType("double precision") + .HasColumnName("pump1Flow"); + + b.Property("Pump2Flow") + .HasColumnType("double precision") + .HasColumnName("pump2Flow"); + + b.Property("RotorSpeed") + .HasColumnType("double precision") + .HasColumnName("rotorSpeed"); + + b.Property("RotorTorque") + .HasColumnType("double precision") + .HasColumnName("rotorTorque"); + + b.Property("TimeStamp") + .HasColumnType("integer") + .HasColumnName("timestamp"); + + b.Property("User") + .HasColumnType("text") + .HasColumnName("user"); + + b.Property("WellDepth") + .HasColumnType("double precision") + .HasColumnName("wellDepth"); + + b.HasKey("Id"); + + b.ToTable("DataSaub"); + }); + + modelBuilder.Entity("Persistence.Database.Model.Setpoint", b => + { + b.Property("Key") + .HasColumnType("uuid") + .HasComment("Ключ"); + + b.Property("Created") + .HasColumnType("timestamp with time zone") + .HasComment("Дата изменения уставки"); + + b.Property("IdUser") + .HasColumnType("integer") + .HasComment("Id автора последнего изменения"); + + b.Property("Value") + .IsRequired() + .HasColumnType("jsonb") + .HasComment("Значение уставки"); + + b.HasKey("Key", "Created"); + + b.ToTable("Setpoint"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Persistence.Database.Postgres/Persistence.Database.Postgres.csproj b/Persistence.Database.Postgres/Persistence.Database.Postgres.csproj new file mode 100644 index 0000000..6b7cc14 --- /dev/null +++ b/Persistence.Database.Postgres/Persistence.Database.Postgres.csproj @@ -0,0 +1,21 @@ + + + + net8.0 + enable + enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/Persistence.Database/Model/PersistenceDbContext.cs b/Persistence.Database.Postgres/PersistenceDbContext.cs similarity index 72% rename from Persistence.Database/Model/PersistenceDbContext.cs rename to Persistence.Database.Postgres/PersistenceDbContext.cs index 5f4b965..75872bc 100644 --- a/Persistence.Database/Model/PersistenceDbContext.cs +++ b/Persistence.Database.Postgres/PersistenceDbContext.cs @@ -1,18 +1,34 @@ using Microsoft.EntityFrameworkCore; +using System.Data.Common; namespace Persistence.Database.Model; public partial class PersistenceDbContext : DbContext, IPersistenceDbContext { + private readonly DbConnection connection = null!; public DbSet DataSaub => Set(); public DbSet Setpoint => Set(); - public PersistenceDbContext() - { - } + public PersistenceDbContext() + : base() + { + + } public PersistenceDbContext(DbContextOptions options) + : base(options) { + + } + + public PersistenceDbContext(DbConnection connection) + { + this.connection = connection; + } + + protected virtual DbConnection GetConnection() + { + return connection; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) @@ -29,5 +45,5 @@ public partial class PersistenceDbContext : DbContext, IPersistenceDbContext .HasAnnotation("Relational:Collation", "Russian_Russia.1251"); } - + } diff --git a/Persistence.Database.Postgres/Readme.md b/Persistence.Database.Postgres/Readme.md new file mode 100644 index 0000000..756a8f5 --- /dev/null +++ b/Persistence.Database.Postgres/Readme.md @@ -0,0 +1,5 @@ +## Создать миграцию +``` +dotnet ef migrations add --project Persistence.Database.Postgres + +``` \ No newline at end of file diff --git a/Persistence.Database/Model/DataSaub.cs b/Persistence.Database/Entity/DataSaub.cs similarity index 100% rename from Persistence.Database/Model/DataSaub.cs rename to Persistence.Database/Entity/DataSaub.cs diff --git a/Persistence.Database/Model/ISetpointData.cs b/Persistence.Database/Entity/ISetpointData.cs similarity index 100% rename from Persistence.Database/Model/ISetpointData.cs rename to Persistence.Database/Entity/ISetpointData.cs diff --git a/Persistence.Database/Model/ITimestampedData.cs b/Persistence.Database/Entity/ITimestampedData.cs similarity index 100% rename from Persistence.Database/Model/ITimestampedData.cs rename to Persistence.Database/Entity/ITimestampedData.cs diff --git a/Persistence.Database/Model/Setpoint.cs b/Persistence.Database/Entity/Setpoint.cs similarity index 100% rename from Persistence.Database/Model/Setpoint.cs rename to Persistence.Database/Entity/Setpoint.cs diff --git a/Persistence.Database/Model/SetpointDictionary.cs b/Persistence.Database/Entity/SetpointDictionary.cs similarity index 100% rename from Persistence.Database/Model/SetpointDictionary.cs rename to Persistence.Database/Entity/SetpointDictionary.cs diff --git a/Persistence.Database/IPersistenceDbContext.cs b/Persistence.Database/IPersistenceDbContext.cs new file mode 100644 index 0000000..66f34ff --- /dev/null +++ b/Persistence.Database/IPersistenceDbContext.cs @@ -0,0 +1,8 @@ +using Microsoft.EntityFrameworkCore; +using Persistence.Database.Model; + +namespace Persistence.Database; +public interface IPersistenceDbContext : IDisposable +{ + DbSet DataSaub { get; } +} diff --git a/Persistence.Database/Model/IDbContextManager.cs b/Persistence.Database/Model/IDbContextManager.cs deleted file mode 100644 index a0a6491..0000000 --- a/Persistence.Database/Model/IDbContextManager.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using System; -using System.Collections.Generic; -using System.Data.Common; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Persistence.Database.Model; -public interface IDbContextManager -{ - //IConnectionManager ConnectionManager { get; } - - DbContext GetReadonlyDbContext(); - - DbContext GetDbContext(); - - DbContext CreateAndInitializeNewContext(); - - DbContext CreateAndInitializeNewContext(DbConnection connection); -} diff --git a/Persistence.Database/Persistence.Database.csproj b/Persistence.Database/Persistence.Database.csproj index 4dca930..8154daf 100644 --- a/Persistence.Database/Persistence.Database.csproj +++ b/Persistence.Database/Persistence.Database.csproj @@ -7,11 +7,11 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive - diff --git a/Persistence.Repository/DependencyInjection.cs b/Persistence.Repository/DependencyInjection.cs index e77edd0..6f0c382 100644 --- a/Persistence.Repository/DependencyInjection.cs +++ b/Persistence.Repository/DependencyInjection.cs @@ -1,10 +1,11 @@ -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Persistence.Repositories; using Persistence.Database.Model; using Persistence.Repository.Data; using Persistence.Repository.Repositories; +using Persistence.Database; namespace Persistence.Repository; public static class DependencyInjection @@ -12,17 +13,11 @@ public static class DependencyInjection public static void MapsterSetup() { } - public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) + + public static IServiceCollection AddInfrastructure(this IServiceCollection services) { MapsterSetup(); - string connectionStringName = "DefaultConnection"; - - services.AddDbContext(options => - options.UseNpgsql(configuration.GetConnectionString(connectionStringName))); - - services.AddScoped(provider => provider.GetRequiredService()); - services.AddTransient, TimeSeriesDataRepository>(); services.AddTransient(); diff --git a/Persistence.Repository/Repositories/TimeSeriesDataRepository.cs b/Persistence.Repository/Repositories/TimeSeriesDataRepository.cs index 97dd358..252888c 100644 --- a/Persistence.Repository/Repositories/TimeSeriesDataRepository.cs +++ b/Persistence.Repository/Repositories/TimeSeriesDataRepository.cs @@ -4,9 +4,10 @@ using Persistence.Models; using Persistence.Repositories; using Persistence.Database.Model; using Persistence.Repository.Data; +using Persistence.Database; namespace Persistence.Repository.Repositories; -public abstract class TimeSeriesDataRepository : ITimeSeriesDataRepository +public class TimeSeriesDataRepository : ITimeSeriesDataRepository where TEntity : class where TDto : class, ITimeSeriesAbstractDto, new() { @@ -17,7 +18,7 @@ public abstract class TimeSeriesDataRepository : ITimeSeriesDataR this.db = db; } - protected virtual IQueryable GetQueryReadOnly() => db.Set(); + protected virtual IQueryable GetQueryReadOnly() => this.db.Set(); public async Task> GetAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token) { diff --git a/Persistence.Repository/Startup.cs b/Persistence.Repository/Startup.cs deleted file mode 100644 index 1d57cbb..0000000 --- a/Persistence.Repository/Startup.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Persistence.Database; -using Persistence.Database.Model; - -namespace Persistence.Repository; -public class Startup -{ - public static void BeforeRunHandler(IHost host) - { - using var scope = host.Services.CreateScope(); - var provider = scope.ServiceProvider; - - var context = provider.GetRequiredService(); - context.Database.EnsureCreatedAndMigrated(); - - } -} diff --git a/Persistence.sln b/Persistence.sln index a3e9f69..ce44190 100644 --- a/Persistence.sln +++ b/Persistence.sln @@ -11,7 +11,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Persistence.Repository", "P EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Persistence.Database", "Persistence.Database\Persistence.Database.csproj", "{F77475D1-D074-407A-9D69-2FADDDAE2056}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Persistence.IntegrationTests", "Persistence.IntegrationTests\Persistence.IntegrationTests.csproj", "{10752C25-3773-4081-A1F2-215A1D950126}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Persistence.IntegrationTests", "Persistence.IntegrationTests\Persistence.IntegrationTests.csproj", "{10752C25-3773-4081-A1F2-215A1D950126}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Persistence.Database.Postgres", "Persistence.Database.Postgres\Persistence.Database.Postgres.csproj", "{CC284D27-162D-490C-B6CF-74D666B7C5F3}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {10752C25-3773-4081-A1F2-215A1D950126}.Debug|Any CPU.Build.0 = Debug|Any CPU {10752C25-3773-4081-A1F2-215A1D950126}.Release|Any CPU.ActiveCfg = Release|Any CPU {10752C25-3773-4081-A1F2-215A1D950126}.Release|Any CPU.Build.0 = Release|Any CPU + {CC284D27-162D-490C-B6CF-74D666B7C5F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CC284D27-162D-490C-B6CF-74D666B7C5F3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CC284D27-162D-490C-B6CF-74D666B7C5F3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CC284D27-162D-490C-B6CF-74D666B7C5F3}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE