Невозможно создать объект типа IdentityDataContext.C#

Место общения программистов C#
Ответить
Anonymous
 Невозможно создать объект типа IdentityDataContext.

Сообщение Anonymous »

Я столкнулся с проблемой при попытке обновить базу данных с помощью Entity Framework Core в своем проекте .NET. Когда я запускаю команду:

Код: Выделить всё

dotnet ef database update --context IdentityDataContext
Я получаю следующее сообщение об ошибке:

Код: Выделить всё

Build started...
Build succeeded.
Unable to create an object of type 'IdentityDataContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
Я настроил свой IdentityDataContext для использования PostgreSQL в своем классе DependencyInjection, как показано ниже:

Код: Выделить всё

public static class DependencyInjection
{
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
{
var connectionString = configuration.GetConnectionString("LoginPostgres");

services.AddDbContext(options =>
{
options.UseNpgsql(connectionString);
});

services.AddDbContext(options =>
{
options.UseNpgsql(connectionString);
});

services.AddIdentity(options =>
{
options.Password.RequireDigit = false;
options.Password.RequiredLength = 4;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequireLowercase = false;
options.SignIn.RequireConfirmedAccount = false;
options.User.RequireUniqueEmail = true;
}).AddEntityFrameworkStores().AddDefaultTokenProviders();

services.AddScoped();

services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

services.AddScoped(typeof(IRepositorioGenerico), typeof(RepositorioGenerico));

services.AddSingleton();
services.AddScoped();
services.AddScoped();
services.AddScoped();

services.AddRazorPages();

services.AddHttpContextAccessor();

return services;
}
}
Контекст данных идентификации файла:

Код: Выделить всё

using Login.Domain.Entities;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Login.Infrastructure.Data.Context
{
public class IdentityDataContext : IdentityDbContext
{
public IdentityDataContext(DbContextOptions options) : base(options)
{
}
}
}
Есть ли у кого-нибудь представление о том, что может быть причиной этой проблемы? Как я могу решить эту проблему?
Заранее спасибо за помощь!

Код: Выделить всё

Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'IdentityDataContext'.  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[Login.Infrastructure.Data.Context.IdentityDataContext]' while attempting to activate 'Login.Infrastructure.Data.Context.IdentityDataContext'.
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__DisplayClass21_4.b__13()
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.c__DisplayClass21_4.b__13()
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.c__DisplayClass0_0.b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Unable to create an object of type 'IdentityDataContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
Entity Framework Core не может разрешить службу Microsoft.EntityFrameworkCore.DbContextOptions при попытке активировать IdentityDataContext.

Подробнее здесь: https://stackoverflow.com/questions/787 ... atacontext
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»