InventoryAgent/Inventory.Core/DesignTimeDbContextFactory.cs

25 lines
947 B
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using DotNetEnv;
namespace Inventory.Core
{
/// <summary>
/// This factory is used by the EF Core design-time tools (e.g., for creating migrations).
/// It provides a way to configure and create an instance of the DbContext at design time.
/// </summary>
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<InventoryContext>
{
public InventoryContext CreateDbContext(string[] args)
{
// Load environment variables from .env file up the directory tree
Env.TraversePath().Load();
string connectionString = Env.GetString("DB_CONNECTION_STRING");
var optionsBuilder = new DbContextOptionsBuilder<InventoryContext>();
optionsBuilder.UseSqlServer(connectionString);
return new InventoryContext(optionsBuilder.Options);
}
}
}