-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to create a 'DbContext' of type ''. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[Project3.Data.TestContext]' while attempting to activate 'Project3.Data.TestContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time #32936
Comments
Try removing the following from your AddDbContext call: |
After removing it, it seems that it cannot be migrated |
@ssccinng you're going to have to post a minimal, runnable code sample for the migration error. In any case, specifying ServiceLifetime.Singleton for the context really is an error. |
https://github.com/ssccinng/TestEFWpf It has been pushed to the project. Just now, what I meant was that removing these codes doesn't seem to make the migration successful |
|
Note for triage: this looks like a duplicate of #32835. That is, |
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: repository_pattern.Services.IAuthentication Lifetime: Scoped ImplementationType: repository_pattern.Services.AuthServices': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager experiencing this error when trying to migrate |
/cc @davidfowl More people struggling with your pattern here. |
I found that doing the migrations with the In my case I found out ef core migration can't read environment variables. I constructed my connection string using values I stored in the For anyone interested: |
I am facing the same error here. My project Mode LastWriteTime Length Name d----- 27/09/2024 21:36 .github my program.cs using ContactZone.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Configure DbContext with connection string and specify migrations assembly
builder.Services.AddDbContext<ContactZoneDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("ContactZone"),
b => b.MigrationsAssembly("ContactZone.Infrastructure")));
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run(); My DbContext using ContactZone.Domain.Domains;
using ContactZone.Infrastructure.Data.FluentMap;
using Microsoft.EntityFrameworkCore;
namespace ContactZone.Infrastructure.Data
{
public class ContactZoneDbContext : DbContext
{
public DbSet<ContactDomain> Contatos { get; set; }
public DbSet<ContactPersonalDataDomain> DadosPessoais { get; set; }
public ContactZoneDbContext(DbContextOptions<ContactZoneDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new ContactMap());
modelBuilder.ApplyConfiguration(new ContactPersonalDataMap());
}
}
} My program is in ContactZone.Api and my dbContext in ContactZone.Infrastructure When I try to migrate, I get this error. |
have you found answer? |
I used information from dotnet/efcore#32936 , dotnet/efcore#32835
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// optionsBuilder.UseSqlServer(connectString); // Use this code when migrating
base.OnConfiguring(optionsBuilder);
} then delete at runtime |
I have solved the problem. It was easier than I thought. Here the problem is on the nuget console the project selected wasn't the same project where my dbContext was. But I needed to click on the box to change the project, I tried to cd and It didn't work |
To those ones who have multiples projects that refer to the same solution, place this code inside a class within the same folder your data layer is: Notice that
|
Make sure that YourContext_DbContext is Concrete: Change YourContext_DbContext from an abstract class to a concrete class. You need to remove the abstract keyword and ensure it has a valid constructor. using Microsoft.EntityFrameworkCore; namespace YourNamespace
} |
Ask a question
Remember:
I want to add database services in WPF through AddDbContext, but I found that this will result in migration failure
Include your code
https://github.com/ssccinng/TestEFWpf
Include verbose output
Unable to create a 'DbContext' of type ''. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions
1[Project3.Data.TestContext]' while attempting to activate 'Project3.Data.TestContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728`Include provider and version information
EF Core version:8.0.0
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 8.0)
Operating system:
IDE: (e.g. Visual Studio 17.9.0 Preview 3.0)
The text was updated successfully, but these errors were encountered: