I have two projects.
1) .NetCore Console App
- Microsoft.EntityFrameworkCore.Tools 2.0.0
- Microsoft.EntityFrameworkCore.SqlServer 2.0.0
- Microsoft.Extensions.Configuration.Json 2.0.0
I have a an entry point
class Program
{
private static IConfiguration Configuration { get; set; }
static void Main(string[] args)
{
IConfigurationRoot configurationRoot = new ConfigurationBuilder()
.AddJsonFile($"appsettings.json", true, true)
.Build();
Configuration = configurationRoot;
// create service collection
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
// create service provider
serviceCollection.BuildServiceProvider();
}
private static void ConfigureServices(ServiceCollection serviceCollection)
{
string connectionString = Configuration.GetConnectionString("DefaultConnection");
serviceCollection.AddDbContext<MyDbContext>(options => options.UseSqlServer(connectionString));
}
}
and an appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "****"
}
}
2) .NetStandard Lib (contains DbContext)
- Microsoft.EntityFrameworkCore 2.0.0
- Microsoft.EntityFrameworkCore.Tools 2.0.0
- Microsoft.EntityFrameworkCore.SqlServer 2.0.0
I want to be able to create a migration script using dotnet ef migrations add [NAME] and i've worked out if I run the command whilst in the lib folder I can target a startup project to run the migrations.
D:\Lib> dotnet ef migrations add InitialCreate -s ..\ConsoleApp\
I managed to get this working by adding a class in the same folder as MyDbContext that implements IDesignTimeDbContextFactory<MyDbContext> I can add a connection string and it runs perfectly.
public class MyDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
{
public MyDbContext CreateDbContext(string[] args)
{
DbContextOptionsBuilder<MyDbContext> builder = new DbContextOptionsBuilder<MyDbContext>();
builder.UseSqlServer("****", optionsBuilder => optionsBuilder.MigrationsAssembly(typeof(MyDbContext).GetTypeInfo().Assembly.GetName().Name));
return new MyDbContext(builder.Options);
}
}
The problem I have is I want to use the connection string from within the console app's appsettings.json.
Is it possible and how can I do it?
Thanks in advance.