Skip to content

Commit 3d840db

Browse files
committed
Fix EF migrations in ASP.NET 2
Per this post: dotnet/aspnetcore#2213
1 parent 9303ee4 commit 3d840db

File tree

4 files changed

+65
-145
lines changed

4 files changed

+65
-145
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore.Design;
3+
using Microsoft.EntityFrameworkCore.Infrastructure;
4+
using Microsoft.Extensions.Configuration;
5+
6+
namespace aspnetCoreReactTemplate.Models
7+
{
8+
public class BloggingContextFactory : IDesignTimeDbContextFactory<DefaultDbContext>
9+
{
10+
public DefaultDbContext CreateDbContext(string[] args)
11+
{
12+
var config = new ConfigurationBuilder()
13+
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
14+
.AddJsonFile("appsettings.json")
15+
.Build();
16+
17+
var options = new DbContextOptionsBuilder<DefaultDbContext>();
18+
options.UseNpgsql(config.GetConnectionString("defaultConnection"));
19+
20+
return new DefaultDbContext(options.Options);
21+
}
22+
}
23+
}

api/Program.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.AspNetCore;
33
using Microsoft.AspNetCore.Hosting;
44
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.DependencyInjection;
56

67
namespace aspnetCoreReactTemplate
78
{
@@ -14,10 +15,24 @@ public static void Main(string[] args)
1415
.AddJsonFile("appsettings.json")
1516
.Build();
1617

17-
BuildWebHost(config["serverBindingUrl"], args).Run();
18+
var host = BuildHost(config["serverBindingUrl"], args);
19+
using (var scope = host.Services.CreateScope())
20+
{
21+
var dbInitializer = scope.ServiceProvider.GetRequiredService<Models.IDefaultDbContextInitializer>();
22+
var env = scope.ServiceProvider.GetRequiredService<IHostingEnvironment>();
23+
// Apply any pending migrations
24+
dbInitializer.Migrate();
25+
if (env.IsDevelopment())
26+
{
27+
// Seed the database in development mode
28+
dbInitializer.Seed().GetAwaiter().GetResult();
29+
}
30+
}
31+
32+
host.Run();
1833
}
1934

20-
public static IWebHost BuildWebHost(string serverBindingUrl, string[] args) =>
35+
public static IWebHost BuildHost(string serverBindingUrl, string[] args) =>
2136
WebHost.CreateDefaultBuilder(args)
2237
.UseContentRoot(Directory.GetCurrentDirectory())
2338
.UseUrls(serverBindingUrl)

api/Startup.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void ConfigureServices(IServiceCollection services)
5050
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
5151
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
5252
})
53-
53+
5454
.AddJwtBearer(config =>
5555
{
5656
config.RequireHttpsMetadata = false;
@@ -76,27 +76,21 @@ public void ConfigureServices(IServiceCollection services)
7676
}
7777

7878
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
79-
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IDefaultDbContextInitializer databaseInitializer)
79+
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
8080
{
8181
// Log to console (stdout) - in production stdout will be written to /var/log/{{app_name}}.out.log
8282
loggerFactory.AddConsole(Configuration.GetSection("logging"));
8383
loggerFactory.AddDebug();
8484

85-
// Apply any pending migrations
86-
// Do not call EnsureCreated() b/c it does not log to _EFMigrationsHistory table (Ref: https://github.com/aspnet/EntityFramework/issues/3875)
87-
databaseInitializer.Migrate();
88-
8985
if (env.IsDevelopment())
9086
{
91-
databaseInitializer.Seed().GetAwaiter().GetResult();
92-
9387
// Configure Webpack Middleware (Ref: http://blog.stevensanderson.com/2016/05/02/angular2-react-knockout-apps-on-aspnet-core/)
9488
// - Intercepts requests for webpack bundles and routes them through Webpack - this prevents needing to run Webpack file watcher separately
9589
// - Enables Hot module replacement (HMR)
9690
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
9791
{
9892
HotModuleReplacement = true,
99-
HotModuleReplacementClientOptions = new Dictionary<string, string> {{ "reload", "true" }},
93+
HotModuleReplacementClientOptions = new Dictionary<string, string> {{ "reload", "true" }},
10094
ReactHotModuleReplacement = true,
10195
ConfigFile = System.IO.Path.Combine(Configuration["webClientPath"], "webpack.config.js")
10296
});

package-lock.json

Lines changed: 22 additions & 134 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)