Do not initialize DBs with test data
Adding sample code to initialize test data is beyond the purview of the Initializr. [Fixes #40]
This commit is contained in:
Родитель
3cb0bb5815
Коммит
9e9cd63812
|
@ -22,10 +22,6 @@ manifest:
|
|||
dependencies: any-efcore
|
||||
- path: Models/ErrorViewModel.cs
|
||||
dependencies: any-efcore
|
||||
- path: Models/InitializeContext.cs
|
||||
dependencies: any-efcore
|
||||
- path: Models/SampleData.cs
|
||||
dependencies: any-efcore
|
||||
- path: Models/TestContext.cs
|
||||
dependencies: any-efcore
|
||||
- path: Properties/
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace {{Namespace}}
|
||||
{
|
||||
public static class InitializeContext
|
||||
{
|
||||
public static IWebHost InitializeDbContexts(this IWebHost host)
|
||||
{
|
||||
using (var scope = host.Services.CreateScope())
|
||||
{
|
||||
var services = scope.ServiceProvider;
|
||||
try
|
||||
{
|
||||
SampleData.InitializeMyContexts(services);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var logger = services.GetRequiredService<ILogger<WebHostBuilder>>();
|
||||
logger.LogError(ex, "An error occurred seeding the DB.");
|
||||
}
|
||||
}
|
||||
return host;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace {{Namespace}}
|
||||
{
|
||||
public class SampleData
|
||||
{
|
||||
internal static void InitializeMyContexts(IServiceProvider serviceProvider)
|
||||
{
|
||||
if (serviceProvider == null)
|
||||
{
|
||||
throw new ArgumentNullException("serviceProvider");
|
||||
}
|
||||
using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
|
||||
{
|
||||
var db = serviceScope.ServiceProvider.GetService<TestContext>();
|
||||
db.Database.EnsureCreated();
|
||||
}
|
||||
InitializeContext(serviceProvider);
|
||||
}
|
||||
|
||||
private static void InitializeContext(IServiceProvider serviceProvider)
|
||||
{
|
||||
using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
|
||||
{
|
||||
var db = serviceScope.ServiceProvider.GetService<TestContext>();
|
||||
if (db.TestData.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
AddData<TestData>(db, new TestData() { Id = 1, Data = "Test Data 1 - TestContext " });
|
||||
AddData<TestData>(db, new TestData() { Id = 2, Data = "Test Data 2 - TestContext " });
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddData<TData>(DbContext db, object item) where TData: class
|
||||
{
|
||||
db.Entry(item).State = EntityState.Added;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -34,9 +34,6 @@ namespace {{Namespace}}
|
|||
{
|
||||
CreateWebHostBuilder(args)
|
||||
.Build()
|
||||
{{#any-efcore}}
|
||||
.InitializeDbContexts()
|
||||
{{/any-efcore}}
|
||||
.Run();
|
||||
|
||||
}
|
||||
|
|
|
@ -20,10 +20,6 @@ manifest:
|
|||
- path: Models/
|
||||
- path: Models/ErrorViewModel.cs
|
||||
dependencies: any-efcore
|
||||
- path: Models/InitializeContext.cs
|
||||
dependencies: any-efcore
|
||||
- path: Models/SampleData.cs
|
||||
dependencies: any-efcore
|
||||
- path: Models/TestContext.cs
|
||||
dependencies: any-efcore
|
||||
- path: Properties/
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace {{Namespace}}
|
||||
{
|
||||
public static class InitializeContext
|
||||
{
|
||||
public static IWebHost InitializeDbContexts(this IWebHost host)
|
||||
{
|
||||
using (var scope = host.Services.CreateScope())
|
||||
{
|
||||
var services = scope.ServiceProvider;
|
||||
try
|
||||
{
|
||||
SampleData.InitializeMyContexts(services);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var logger = services.GetRequiredService<ILogger<WebHostBuilder>>();
|
||||
logger.LogError(ex, "An error occurred seeding the DB.");
|
||||
}
|
||||
}
|
||||
return host;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace {{Namespace}}
|
||||
{
|
||||
public class SampleData
|
||||
{
|
||||
internal static void InitializeMyContexts(IServiceProvider serviceProvider)
|
||||
{
|
||||
if (serviceProvider == null)
|
||||
{
|
||||
throw new ArgumentNullException("serviceProvider");
|
||||
}
|
||||
using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
|
||||
{
|
||||
var db = serviceScope.ServiceProvider.GetService<TestContext>();
|
||||
db.Database.EnsureCreated();
|
||||
}
|
||||
InitializeContext(serviceProvider);
|
||||
}
|
||||
|
||||
private static void InitializeContext(IServiceProvider serviceProvider)
|
||||
{
|
||||
using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
|
||||
{
|
||||
var db = serviceScope.ServiceProvider.GetService<TestContext>();
|
||||
if (db.TestData.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
AddData<TestData>(db, new TestData() { Id = 1, Data = "Test Data 1 - TestContext " });
|
||||
AddData<TestData>(db, new TestData() { Id = 2, Data = "Test Data 2 - TestContext " });
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddData<TData>(DbContext db, object item) where TData: class
|
||||
{
|
||||
db.Entry(item).State = EntityState.Added;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -37,9 +37,6 @@ namespace {{Namespace}}
|
|||
{
|
||||
CreateWebHostBuilder(args)
|
||||
.Build()
|
||||
{{#any-efcore}}
|
||||
.InitializeDbContexts()
|
||||
{{/any-efcore}}
|
||||
.Run();
|
||||
|
||||
}
|
||||
|
|
|
@ -20,10 +20,6 @@ manifest:
|
|||
- path: Models/
|
||||
- path: Models/ErrorViewModel.cs
|
||||
dependencies: any-efcore
|
||||
- path: Models/InitializeContext.cs
|
||||
dependencies: any-efcore
|
||||
- path: Models/SampleData.cs
|
||||
dependencies: any-efcore
|
||||
- path: Models/TestContext.cs
|
||||
dependencies: any-efcore
|
||||
- path: Properties/
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace {{Namespace}}
|
||||
{
|
||||
public static class InitializeContext
|
||||
{
|
||||
public static IWebHost InitializeDbContexts(this IWebHost host)
|
||||
{
|
||||
using (var scope = host.Services.CreateScope())
|
||||
{
|
||||
var services = scope.ServiceProvider;
|
||||
try
|
||||
{
|
||||
SampleData.InitializeMyContexts(services);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var logger = services.GetRequiredService<ILogger<WebHostBuilder>>();
|
||||
logger.LogError(ex, "An error occurred seeding the DB.");
|
||||
}
|
||||
}
|
||||
return host;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace {{Namespace}}
|
||||
{
|
||||
public class SampleData
|
||||
{
|
||||
internal static void InitializeMyContexts(IServiceProvider serviceProvider)
|
||||
{
|
||||
if (serviceProvider == null)
|
||||
{
|
||||
throw new ArgumentNullException("serviceProvider");
|
||||
}
|
||||
using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
|
||||
{
|
||||
var db = serviceScope.ServiceProvider.GetService<TestContext>();
|
||||
db.Database.EnsureCreated();
|
||||
}
|
||||
InitializeContext(serviceProvider);
|
||||
}
|
||||
|
||||
private static void InitializeContext(IServiceProvider serviceProvider)
|
||||
{
|
||||
using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
|
||||
{
|
||||
var db = serviceScope.ServiceProvider.GetService<TestContext>();
|
||||
if (db.TestData.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
AddData<TestData>(db, new TestData() { Id = 1, Data = "Test Data 1 - TestContext " });
|
||||
AddData<TestData>(db, new TestData() { Id = 2, Data = "Test Data 2 - TestContext " });
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddData<TData>(DbContext db, object item) where TData: class
|
||||
{
|
||||
db.Entry(item).State = EntityState.Added;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -30,9 +30,6 @@ namespace {{Namespace}}
|
|||
{
|
||||
CreateWebHostBuilder(args)
|
||||
.Build()
|
||||
{{#any-efcore}}
|
||||
.InitializeDbContexts()
|
||||
{{/any-efcore}}
|
||||
.Run();
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче