Search code examples
sql-server.net-corewindows-services

Using SQL Server with dotnet core 6.0 in a Windows Service


I am trying to add SQL Server connections to a new Windows Service. All the documentation indicates that we add

builder.Services.AddDbContext<ApplicationContext> 

in Program.cs.

However a Windows service (IHost) does not show the AddDbContext method in services. Does anyone have any experience with this?

Already been here: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-6.0&tabs=visual-studio

and here: https://jasonwatmore.com/post/2022/03/18/net-6-connect-to-sql-server-with-entity-framework-core @JasonWatmore

and similar....


Solution

  • You should use AddDbContext instead of AddDbContact. Try it; services.AddDbContext<ApplicationContext>();

    Also make sure you have these packages installed;

    Install-Package Microsoft.EntityFrameworkCore -Version 6.0.8

    Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 6.0.8