This can be easily achieved by having a constructor on the DbContext that takes an instance of DbContextOptions as an argument and using the AddDbContext method. The DbContext itself and any dependencies in its constructor need to be registered as services in the application's service provider. When you create a new ASP.NET Core application, this hook is included by default. Public ApplicationDbContext(DbContextOptions options) Public class ApplicationDbContext : DbContext Public void Configure(IApplicationBuilder app, IWebHostEnvironment env) Public void ConfigureServices(IServiceCollection services) Public static IHostBuilder CreateHostBuilder(string args) EF Core uses this method at design time to access the DbContext => CreateHostBuilder(args).Build().Run() The tools first try to obtain the service provider by invoking Program.CreateHostBuilder(), calling Build(), then accessing the Services property. NET Core Generic Host, the tools try to obtain the DbContext object from the application's service provider. If your startup project uses the ASP.NET Core Web Host or. There are various ways the tools try to create the DbContext: From application services In most cases, it is desirable that the DbContext thereby created is configured in a similar way to how it would be configured at run time. Some of the EF Core Tools commands (for example, the Migrations commands) require a derived DbContext instance to be created at design time in order to gather details about the application's entity types and how they map to a database schema.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |