Service für Typ 'microsoft.entityFrameworkCore.dbContextOptions' kann nicht behoben werden.C#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Service für Typ 'microsoft.entityFrameworkCore.dbContextOptions' kann nicht behoben werden.

Post by Anonymous »

Ich versuche, mit EF Core 5.0 in einem ASP.NET -Kernprojekt auf die Datenbank zuzugreifen. Für die erste Migration habe ich die Methode onconfiguring () im dbcontext überschrieben und die Datenbank erfolgreich aktualisiert. Hier sind die Änderungen, die ich vorgenommen habe. Beiträge , ich erhalte folgende Fehler:

Code: Select all

Microsoft.EntityFrameworkCore.Design.OperationException:
Unable to create an object of type 'BlogContext'.
For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
< /code>
Wenn ich das Flag --verbose füge, erhalte ich folgende Ausgabe: < /p>
Build started...
dotnet build blog/app/app.csproj /verbosity:quiet /nologo

Build succeeded.
0 Warning(s)
0 Error(s)

Time Elapsed 00:00:02.50
Build succeeded.

Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'app'...
Finding Microsoft.Extensions.Hosting service provider...
No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'BlogContext'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'BlogContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[app.Data.BlogContext]' while attempting to activate 'app.Data.BlogContext'.
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.c__DisplayClass13_4.b__13()

Unable to create an object of type 'BlogContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Der Code funktioniert jedoch wie erwartet, wenn ich die Webanwendung ausführe, wie im BlogContext erstellt und in meine Klassen durch die DI -Schicht injiziert und auf die Datenbank zugreifen kann. Mein Code. < /p>

Code: Select all

// Program.cs

public class Program
{
public static void Main(string[] args)
{
IHostBuilder builder = Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup();
});

IHost host = builder.Build();

host.Run();
}
}

// BlogContext

using app.Models;
using Microsoft.EntityFrameworkCore;

namespace app.Data
{
public class BlogContext : DbContext
{
public DbSet
 Posts { get; set; }

public DbSet Comments { get; set; }

public BlogContext(DbContextOptions options) : base(options)
{

}
}
}

// Startup.cs

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public string ConnectionString => Configuration.GetConnectionString("Blog");

public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext(opt => { opt.UseSqlite(ConnectionString); });
}
}
Beide Start -up und BlogContext Live im selben Projekt.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post