Skip to content

Commit

Permalink
fix migrations location
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-venicius committed Jun 11, 2023
1 parent 1633ce0 commit c03f1d8
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 27 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@ click on "Use this template" to start

## Create migrations

inside the solution folder

```shell
dotnet-ef migrations add <migration_name> -p src/Infra/Infra.csproj -s src/Core/Core.csproj
```

## Update database

inside the solution folder

```shell
dotnet-ef migrations add <migration_name> -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
Expand Down
1 change: 1 addition & 0 deletions src/Application/Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="FluentValidation" Version="11.5.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
</ItemGroup>

<PropertyGroup>
Expand Down
1 change: 1 addition & 0 deletions src/Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Application\Application.csproj" />
<ProjectReference Include="..\Infra\Infra.csproj" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/Core/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddInfra();
builder.Services.AddInfrastructure(builder.Configuration);

var app = builder.Build();

Expand Down
4 changes: 2 additions & 2 deletions src/Infra/Common/MediatorExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Domain.Common;
using Infra.Context;
using Infra.Persistence;
using MediatR;

namespace Infra.Common;
Expand All @@ -22,4 +22,4 @@ public static async Task DispatchDomainEvents(this IMediator mediator, EFContext
foreach (var domainEvent in domainEvents)
await mediator.Publish(domainEvent);
}
}
}
13 changes: 9 additions & 4 deletions src/Infra/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EFContext>();
services.AddDbContext<EFContext>(options => options.UseSqlite(
configuration.GetConnectionString("sqlite"),
b => b.MigrationsAssembly(typeof(EFContext).Assembly.FullName)),
ServiceLifetime.Transient
);

services.AddScoped<IEFContext>(provider => provider.GetRequiredService<EFContext>());

Expand Down Expand Up @@ -68,4 +73,4 @@ public static IServiceCollection AddInfra(this IServiceCollection services)

return services;
}
}
}
6 changes: 5 additions & 1 deletion src/Infra/Infra.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Domain\Domain.csproj" />
<ProjectReference Include="..\Application\Application.csproj" />
</ItemGroup>

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

#nullable disable

namespace Core.Infra.Data.Migrations
namespace Infra.Migrations
{
/// <inheritdoc />
public partial class Initial : Migration
public partial class Initial_Migration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// <auto-generated />
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,22 @@
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<EFContext> options, IConfiguration configuration, IMediator mediator)
: base(options)
{
_cofiguration = configuration;
_mediator = mediator;
}

public DbSet<Todo> 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
Expand Down Expand Up @@ -58,4 +52,4 @@ public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess,

return await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}
}
}

0 comments on commit c03f1d8

Please sign in to comment.