Instantly share code, notes, and snippets.

Embed
What would you like to do?
Factory need to use migration in .Net Core 2.1
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
namespace StoreOfBuild.Data.Contexts
{
public class ContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext()
{
return CreateDbContext(null);
}
public ApplicationDbContext CreateDbContext(string[] args)
{
var builderConfiguration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.Development.json");
var configuration = builderConfiguration.Build();
var connectionString = configuration.GetConnectionString("DefaultConnection");
var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
builder.UseSqlServer(connectionString);
return new ApplicationDbContext(builder.Options);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment