相关疑难解决方法(0)

如何以及在何处调用Database.EnsureCreated和Database.Migrate?

我有一个ASP.NET MVC 6应用程序,我需要调用Database.EnsureCreated和Database.Migrate方法.

但我应该在哪里打电话给他们?

asp.net asp.net-mvc entity-framework entity-framework-core

59
推荐指数
6
解决办法
4万
查看次数

How to access DbContext in .NET 6 minimal API Program.cs

I am trying to call EF Core methods on application startup in my Program.cs file, using the .NET 6 minimal API template and get the following error:

System.InvalidOperationException: 'Cannot resolve scoped service 'Server.Infrastructure.DbContexts.AppDbContext' from root provider.'

// ************** Build Web Application **************

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDbContext<AppDbContext>(options =>
    options.UseNpgsql(configuration.GetConnectionString("AppDb:Postgres")));

// ...

// **************** Web Application *****************

var app = builder.Build();

var dbContext = app.Services.GetService<AppDbContext>(); // error thrown here

if (dbContext != null)
{
    dbContext.Database.EnsureDeleted();
    dbContext.Database.Migrate();
}

// ...
Run Code Online (Sandbox Code Playgroud)

With earlier …

c# entity-framework-core .net-6.0 minimal-apis

14
推荐指数
1
解决办法
2万
查看次数

无法解析范围服务

我在理解代码中的错误源时遇到了问题。我尝试着讲有关.net核心中的微服务的课程。运行构建解决方案后,我得到:

------- Project finished: CrossX.Services.Identity. Succeeded: True. Errors: 0. Warnings: 0
Run Code Online (Sandbox Code Playgroud)

但是当我运行它时,我得到:

/opt/dotnet/dotnet /RiderProjects/crossx/src/CrossX.Services.Identity/bin/Debug/netcoreapp2.2/CrossX.Services.Identity.dll

Unhandled Exception: System.InvalidOperationException: Cannot resolve scoped service 'CrossX.NETCore.Commands.ICommandHandler`1[CrossX.NETCore.Commands.CreateUser]' from root provider.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(Type serviceType, IServiceScope scope, IServiceScope rootScope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
   at CrossX.NETCore.Services.ServiceHost.BusBuilder.SubscribeToCommand[TCommand]() in /RiderProjects/crossx/src/CrossX.NETCore/Services/ServiceHost.cs:line 78
   at CrossX.Services.Identity.Program.Main(String[] args) in /RiderProjects/crossx/src/CrossX.Services.Identity/Program.cs:line 11
Run Code Online (Sandbox Code Playgroud)

当我添加到webHostBuilder .UseDefaultServiceProvider(options => options.ValidateScopes = false)时,我的问题已解决。但是据我所知,关闭验证并不是一个好主意。另外,当我将AddScope更改为AddTransient时,问题已解决(或至少已运行)。

问题是我不知道在哪里可以找到此错误的来源。我想我不太了解哪里出了问题,因此,如果有人可以帮助我,或者至少提供一个提示,我将不胜感激。

这是我的

Startup.cs:

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

        public IConfiguration Configuration { get; }

        // This method gets called …
Run Code Online (Sandbox Code Playgroud)

c# asp.net .net-core asp.net-core

1
推荐指数
1
解决办法
2016
查看次数

协助将 .NET 5 转换为 .NET 6

我正在尝试为我的数据库添加种子并将角色添加到我的 ASP.NET Core Web 应用程序。在我接下来的教程中,建议将以下内容添加到Configure()方法中:

    app.UseAuthentication();
MyIdentityDataInitializer.SeedData(userManager, roleManager);
Run Code Online (Sandbox Code Playgroud)

我对第一行没有任何问题,但在第二行中,我收到一条错误,指出 userManager 和 roleManager 不存在,这是有道理的,因为如果我使用 .NET 5,我的配置方法将如下所示并且没问题:

public void Configure(IApplicationBuilder app, 
IHostingEnvironment env, 
UserManager<MyIdentityUser> userManager, 
RoleManager<MyIdentityRole> roleManager)
{
    ...
    app.UseAuthentication();

    MyIdentityDataInitializer.SeedData(userManager, roleManager);

    app.UseStaticFiles();

    app.UseMvc(routes =>
    {
      routes.MapRoute(
      name: "default",
      template: "{controller=Home}/{action=Index}/{id?}");
    });
}
Run Code Online (Sandbox Code Playgroud)

但在 .NET 6 中我不知道该怎么做。

有人可以就此提出建议吗?

.net c# asp.net asp.net-core .net-6.0

0
推荐指数
1
解决办法
424
查看次数