Created
June 23, 2018 18:34
-
-
Save StephanyBatista/2f9e1685015e245fa5273753d84b337a to your computer and use it in GitHub Desktop.
Factory need to use migration in .Net Core 2.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
very nice! thank you!