Using SQL Server with dotnet core 6.0 in a Windows Service

Viewed 44

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://docs.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....

2 Answers

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

You are adding the wrong dependency. You need to add this dependency

builder.Services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(connectionString));
Related