Created
May 21, 2025 20:30
-
-
Save roji/d67832f78c3515376f58a20c9b669ca3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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