Skip to content

Instantly share code, notes, and snippets.

@roji
Created May 21, 2025 20:30
Show Gist options
  • Select an option

  • Save roji/d67832f78c3515376f58a20c9b669ca3 to your computer and use it in GitHub Desktop.

Select an option

Save roji/d67832f78c3515376f58a20c9b669ca3 to your computer and use it in GitHub Desktop.
await using var context = new BlogContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
using var ctx = new BlogContext();
var services = new ServiceCollection()
.AddEntityFrameworkDesignTimeServices()
.AddDbContextDesignTimeServices(ctx);
var npgsqlDesignTimeServices = new NpgsqlDesignTimeServices();
npgsqlDesignTimeServices.ConfigureDesignTimeServices(services);
var serviceProvider = services.BuildServiceProvider();
var scaffolder = serviceProvider.GetRequiredService<IMigrationsScaffolder>();
var migration = scaffolder.ScaffoldMigration(
"MyMigration",
"MyApp.Data");
Console.WriteLine(migration.MigrationCode);
public class BlogContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseNpgsql("Host=localhost;Username=test;Password=test")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
}
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
public List<Post> Posts { get; set; }
}
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public Blog Blog { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment