diff --git a/README.md b/README.md index a6c0637..f44c49d 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,18 @@ click on "Use this template" to start ## Create migrations +inside the solution folder + +```shell +dotnet-ef migrations add -p src/Infra/Infra.csproj -s src/Core/Core.csproj +``` + +## Update database + +inside the solution folder + ```shell -dotnet-ef migrations add -o Infra/Data/Migrations --startup-project Core/Core.csproj +dotnet-ef database update -p src/Infra/Infra.csproj -s src/Core/Core.csproj ``` ## Run project diff --git a/src/Application/Application.csproj b/src/Application/Application.csproj index cb74139..a15993e 100644 --- a/src/Application/Application.csproj +++ b/src/Application/Application.csproj @@ -8,6 +8,7 @@ + diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index bb3b3e9..2743f8d 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -17,6 +17,7 @@ + diff --git a/src/Core/Program.cs b/src/Core/Program.cs index f02ee50..bc18b74 100644 --- a/src/Core/Program.cs +++ b/src/Core/Program.cs @@ -7,7 +7,7 @@ builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -builder.Services.AddInfra(); +builder.Services.AddInfrastructure(builder.Configuration); var app = builder.Build(); diff --git a/src/Infra/Common/MediatorExtensions.cs b/src/Infra/Common/MediatorExtensions.cs index d1b2dd5..a359200 100644 --- a/src/Infra/Common/MediatorExtensions.cs +++ b/src/Infra/Common/MediatorExtensions.cs @@ -1,5 +1,5 @@ using Domain.Common; -using Infra.Context; +using Infra.Persistence; using MediatR; namespace Infra.Common; @@ -22,4 +22,4 @@ public static async Task DispatchDomainEvents(this IMediator mediator, EFContext foreach (var domainEvent in domainEvents) await mediator.Publish(domainEvent); } -} \ No newline at end of file +} diff --git a/src/Infra/DependencyInjection.cs b/src/Infra/DependencyInjection.cs index 5d68a4e..a2a9567 100644 --- a/src/Infra/DependencyInjection.cs +++ b/src/Infra/DependencyInjection.cs @@ -2,21 +2,26 @@ using Application.Common.Behaviours; using Application.Common.Interfaces; using FluentValidation; -using Infra.Context; +using Infra.Persistence; using MediatR; using Microsoft.AspNetCore.Mvc.Versioning; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace Infra; public static class DependencyInjection { - public static IServiceCollection AddInfra(this IServiceCollection services) + public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) { services.AddMemoryCache(); - services.AddDbContext(); + services.AddDbContext(options => options.UseSqlite( + configuration.GetConnectionString("sqlite"), + b => b.MigrationsAssembly(typeof(EFContext).Assembly.FullName)), + ServiceLifetime.Transient + ); services.AddScoped(provider => provider.GetRequiredService()); @@ -68,4 +73,4 @@ public static IServiceCollection AddInfra(this IServiceCollection services) return services; } -} \ No newline at end of file +} diff --git a/src/Infra/Infra.csproj b/src/Infra/Infra.csproj index 96494be..fa8843f 100644 --- a/src/Infra/Infra.csproj +++ b/src/Infra/Infra.csproj @@ -18,10 +18,14 @@ all + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + - diff --git a/src/Core/Infra/Data/Migrations/20230604170716_Initial.Designer.cs b/src/Infra/Migrations/20230611105404_Initial_Migration.Designer.cs similarity index 90% rename from src/Core/Infra/Data/Migrations/20230604170716_Initial.Designer.cs rename to src/Infra/Migrations/20230611105404_Initial_Migration.Designer.cs index 8b15514..88e130c 100644 --- a/src/Core/Infra/Data/Migrations/20230604170716_Initial.Designer.cs +++ b/src/Infra/Migrations/20230611105404_Initial_Migration.Designer.cs @@ -1,6 +1,6 @@ // using System; -using Infra.Context; +using Infra.Persistence; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; @@ -8,11 +8,11 @@ #nullable disable -namespace Core.Infra.Data.Migrations +namespace Infra.Migrations { [DbContext(typeof(EFContext))] - [Migration("20230604170716_Initial")] - partial class Initial + [Migration("20230611105404_Initial_Migration")] + partial class Initial_Migration { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) diff --git a/src/Core/Infra/Data/Migrations/20230604170716_Initial.cs b/src/Infra/Migrations/20230611105404_Initial_Migration.cs similarity index 93% rename from src/Core/Infra/Data/Migrations/20230604170716_Initial.cs rename to src/Infra/Migrations/20230611105404_Initial_Migration.cs index 35de1e9..ba78457 100644 --- a/src/Core/Infra/Data/Migrations/20230604170716_Initial.cs +++ b/src/Infra/Migrations/20230611105404_Initial_Migration.cs @@ -3,10 +3,10 @@ #nullable disable -namespace Core.Infra.Data.Migrations +namespace Infra.Migrations { /// - public partial class Initial : Migration + public partial class Initial_Migration : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) diff --git a/src/Core/Infra/Data/Migrations/EFContextModelSnapshot.cs b/src/Infra/Migrations/EFContextModelSnapshot.cs similarity index 95% rename from src/Core/Infra/Data/Migrations/EFContextModelSnapshot.cs rename to src/Infra/Migrations/EFContextModelSnapshot.cs index 5cf72cd..9caf900 100644 --- a/src/Core/Infra/Data/Migrations/EFContextModelSnapshot.cs +++ b/src/Infra/Migrations/EFContextModelSnapshot.cs @@ -1,13 +1,13 @@ // using System; -using Infra.Context; +using Infra.Persistence; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; #nullable disable -namespace Core.Infra.Data.Migrations +namespace Infra.Migrations { [DbContext(typeof(EFContext))] partial class EFContextModelSnapshot : ModelSnapshot diff --git a/src/Infra/Context/EFContext.cs b/src/Infra/Persistence/EFContext.cs similarity index 81% rename from src/Infra/Context/EFContext.cs rename to src/Infra/Persistence/EFContext.cs index fd9ce64..d276020 100644 --- a/src/Infra/Context/EFContext.cs +++ b/src/Infra/Persistence/EFContext.cs @@ -6,14 +6,15 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; -namespace Infra.Context; +namespace Infra.Persistence; public sealed class EFContext : DbContext, IEFContext { private readonly IMediator _mediator; private readonly IConfiguration _cofiguration; - public EFContext(IConfiguration configuration, IMediator mediator) + public EFContext(DbContextOptions options, IConfiguration configuration, IMediator mediator) + : base(options) { _cofiguration = configuration; _mediator = mediator; @@ -21,13 +22,6 @@ public EFContext(IConfiguration configuration, IMediator mediator) public DbSet Todos { get; set; } = default!; - protected override void OnConfiguring(DbContextOptionsBuilder options) - { - var connectionString = _cofiguration.GetConnectionString("sqlite"); - - options.UseSqlite(connectionString, b => b.MigrationsAssembly("Core")); - } - public void AutoUpdateFields() { var entries = ChangeTracker @@ -58,4 +52,4 @@ public override async Task SaveChangesAsync(bool acceptAllChangesOnSuccess, return await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); } -} \ No newline at end of file +}