Skip to content

Commit

Permalink
Fix explicit connection string on DbContex to apply Database Migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
83585-Vanderlan committed Sep 28, 2023
1 parent 8d51c82 commit b348915
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 30 deletions.
26 changes: 15 additions & 11 deletions Orion.Api/Orion.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,32 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
<PackageReference Include="coverlet.msbuild" Version="3.2.0">
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="coverlet.msbuild" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="FluentValidation.AspNetCore" Version="11.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.4" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.11" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Server.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="5.0.17" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Routing.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="7.0.11" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.18.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.5" />
<PackageReference Include="NLog" Version="5.1.2" />
<PackageReference Include="NLog.Web.AspNetCore" Version="5.2.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.10" />
<PackageReference Include="NLog" Version="5.2.4" />
<PackageReference Include="NLog.Web.AspNetCore" Version="5.3.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

Expand Down
11 changes: 6 additions & 5 deletions Orion.Data/Context/DataContext.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using System;
using System.Linq;
using System.Threading.Tasks;
using Orion.Data.Mapping;
using Orion.Entities.Domain;
using Microsoft.Extensions.Configuration;

namespace Orion.Data.Context
{
public class DataContext : DbContext
{
public ModelBuilder ModelBuilder { get; private set; }

public DataContext() : base(GetDefaultOptions())
public DataContext(IConfiguration configuration) : base(GetDefaultOptions(configuration))
{

}

public DataContext(DbContextOptions options) : base(options)
{

Expand All @@ -27,9 +28,9 @@ public DataContext(DbContextOptions options) : base(options)
public DbSet<RefreshToken> RefreshTokens { get; set; }
#endregion

private static DbContextOptions GetDefaultOptions()
private static DbContextOptions GetDefaultOptions(IConfiguration configuration)
{
var connectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=Orion;Integrated Security=True;MultipleActiveResultSets=True;";
var connectionString = configuration.GetSection("DatabaseOptions:ConnectionString").Value;

return SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), connectionString).Options;
}
Expand All @@ -40,7 +41,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.ApplyConfiguration(new UserMapping());
modelBuilder.ApplyConfiguration(new RefreshTokenMapping());

foreach (IMutableEntityType entityType in modelBuilder.Model.GetEntityTypes())
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
modelBuilder.Entity(entityType.ClrType).ToTable(entityType.ClrType.Name);
}
Expand Down
6 changes: 3 additions & 3 deletions Orion.Data/Orion.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.4">
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion Orion.Resources/Orion.Resources.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="7.0.11" />
</ItemGroup>

<ItemGroup>
Expand Down
20 changes: 10 additions & 10 deletions Orion.Test/Orion.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="3.2.0">
<PackageReference Include="coverlet.msbuild" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.4" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="7.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.11" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="7.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.11" />
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="7.0.11" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="xunit" Version="2.5.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.11" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,7 @@ The main objective is to start projects with a clean and simple architecture, wi
**Todo**

1. API Test.

**Run database migrations**

dotnet ef database update -p Orion.Data -s Orion.Api --verbose

0 comments on commit b348915

Please sign in to comment.