public class Post
{
public int Id { get; set; }
public DateTime DatePublished { get; set; }
public string Title { get; set; }
public string Body { get; set; }
}
public class BlogDbContext : DbContext
{
public BlogDbContext(DbContextOptions options)
: base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer(ConexionStringSetting.GetConnectionString());
}
public DbSet<Post> Posts { get; set; }
}
public class AppDataContextFactory : IDesignTimeDbContextFactory<BlogDbContext>
{
public BlogDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<BlogDbContext>();
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
optionsBuilder.UseSqlServer(config.GetConnectionString("Default"));
return new BlogDbContext(optionsBuilder.Options);
}
}
public static class ConexionStringSetting
{
public static string GetConnectionString(string connectionStringName = "Default")
{
string output = "";
var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json");
var config = builder.Build();
output = config.GetConnectionString(connectionStringName);
return output;
}
}
}