Skip to content

Commit c186d5f

Browse files
author
jnance
committed
test(acceptance/no-ef): ensure database is migrated
1 parent 93edba5 commit c186d5f

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

src/NoEntityFrameworkExample/NoEntityFrameworkExample.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
1818
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
1919
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
20+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="1.1.0" />
2021
<PackageReference Include="Npgsql" Version="3.2.2" />
2122
<PackageReference Include="Dapper" Version="1.50.2" />
2223
</ItemGroup>

src/NoEntityFrameworkExample/Startup.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using JsonApiDotNetCore.Extensions;
22
using JsonApiDotNetCore.Services;
3+
using JsonApiDotNetCoreExample.Data;
34
using JsonApiDotNetCoreExample.Models;
45
using Microsoft.AspNetCore.Builder;
56
using Microsoft.AspNetCore.Hosting;
67
using Microsoft.Extensions.Configuration;
78
using Microsoft.Extensions.DependencyInjection;
89
using Microsoft.Extensions.Logging;
910
using NoEntityFrameworkExample.Services;
11+
using Microsoft.EntityFrameworkCore;
1012

1113
namespace NoEntityFrameworkExample
1214
{
@@ -36,17 +38,24 @@ public void ConfigureServices(IServiceCollection services)
3638
builder.AddResource<TodoItem>("custom-todo-items");
3739
});
3840
}, mvcBuilder);
39-
41+
4042
services.AddScoped<IResourceService<TodoItem>, TodoItemService>();
43+
44+
var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
45+
optionsBuilder.UseNpgsql(Configuration.GetValue<string>("Data:DefaultConnection"));
4146
services.AddSingleton<IConfiguration>(Configuration);
47+
services.AddSingleton<DbContextOptions<AppDbContext>>(optionsBuilder.Options);
48+
services.AddScoped<AppDbContext>();
4249
}
4350

4451
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
45-
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
52+
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, AppDbContext context)
4653
{
4754
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
4855
loggerFactory.AddDebug();
4956

57+
context.Database.Migrate();
58+
5059
app.UseMvc();
5160
}
5261
}

test/JsonApiDotNetCoreExampleTests/Acceptance/Extensibility/NoEntityFrameworkTests.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,20 @@
99
using System.Net;
1010
using JsonApiDotNetCoreExample.Models;
1111
using JsonApiDotNetCoreExample.Data;
12-
using Microsoft.EntityFrameworkCore;
13-
using Microsoft.Extensions.Configuration;
1412

1513
namespace JsonApiDotNetCoreExampleTests.Acceptance.Extensibility
1614
{
1715
public class NoEntityFrameworkTests
1816
{
1917
private readonly TestServer _server;
20-
private readonly IConfiguration _config;
2118
private readonly AppDbContext _context;
2219

2320
public NoEntityFrameworkTests()
2421
{
2522
var builder = new WebHostBuilder()
2623
.UseStartup<Startup>();
2724
_server = new TestServer(builder);
28-
_config = _server.GetService<IConfiguration>();
29-
30-
var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
31-
optionsBuilder.UseNpgsql(_config.GetValue<string>("Data:DefaultConnection"));
32-
_context = new AppDbContext(optionsBuilder.Options);
25+
_context = _server.GetService<AppDbContext>();
3326
_context.Database.EnsureCreated();
3427
}
3528

0 commit comments

Comments
 (0)