Per EF Nucleo RTM 1.0 e ASP.NET 1.0 RTM Nucleo
innanzitutto creare il metodo di seme. Ecco perché siamo fuori la portata della richiesta corrente, dobbiamo creare lo manulally:
using System.Collections.Generic;
using System.Linq;
using Core1RtmEmptyTest.Entities;
using Microsoft.Extensions.DependencyInjection;
namespace Core1RtmEmptyTest.Migrations
{
public static class ApplicationDbContextSeedData
{
public static void SeedData(this IServiceScopeFactory scopeFactory)
{
using (var serviceScope = scopeFactory.CreateScope())
{
var context = serviceScope.ServiceProvider.GetService<ApplicationDbContext>();
if (!context.Persons.Any())
{
var persons = new List<Person>
{
new Person
{
FirstName = "Admin",
LastName = "User"
}
};
context.AddRange(persons);
context.SaveChanges();
}
}
}
}
}
quindi specificare il tempo di vita corretta del ApplicationDbContext
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(ServiceLifetime.Scoped);
E, infine richiamato il metodo SeedData()
dal Configure
metodo
public void Configure(IServiceScopeFactory scopeFactory)
{
scopeFactory.SeedData();
È anche possibile vedere questo articolo nei blog MSDN: https://blogs.msdn.microsoft.com/dotnet/2016/09/29/implementing-seeding-custom-conventions-and-interceptors-in-ef-core-1-0/ –