قسمت آخر مقاله یعنی قطعه کد
// Only used by EF Tooling public static IWebHost BuildWebHost(string[] args) { return WebHost.CreateDefaultBuilder() .ConfigureAppConfiguration((ctx, cfg) => { cfg.SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", true) // require the json file! .AddEnvironmentVariables(); }) .ConfigureLogging((ctx, logging) => { }) // No logging .UseStartup() .UseSetting("DesignTime", "true") .Build(); }
UseSetting("DesignTime", "true")
if (_config["DesignTime"] != "true") { using (var scope = app.ApplicationServices.CreateScope()) { var initializer = scope.ServiceProvider.GetRequiredService<YourSampleDataInitializer>(); initializer.RunAsync().Wait(); } }
public class BloggingContextFactory : IDesignTimeDbContextFactory<BloggingContext> { public BloggingContext Create(string[] args) { var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>(); optionsBuilder.UseSqlite("Data Source=blog.db"); return new BloggingContext(optionsBuilder.Options); } }
public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .Build();