D
D
DD-var2021-06-10 21:54:52
ASP.NET
DD-var, 2021-06-10 21:54:52

Why does the migration fail?

I'm trying to make a migration in RIDER, but I keep getting an error with the migration, what exactly is the problem?
Migrating I make dotnet-ef migrations add LibralyReact

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

User:

public class User:IdentityUser
    {        
        public string Password { get; set; }
        public string FirstName { get; set; }
        public string Surname { get; set; }
    }

Application
public sealed class ApplicationContext:IdentityDbContext<User>
    {
        
        public ApplicationContext(DbContextOptions<ApplicationContext> options) : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
            base.OnModelCreating(builder);
        }
    }

adding to service
public static IServiceCollection InitServices(IServiceCollection services, IConfiguration configuration)
        {
            
            services.AddDbContext<ApplicationContext>(
                options => options.UseNpgsql(configuration.GetConnectionString("DefaultConnection"),
                    b => b.MigrationsAssembly("Libraly.Data")
                ));

            services.AddIdentity<User, IdentityRole>().AddRoles<IdentityRole>()
                .AddEntityFrameworkStores<ApplicationContext>();


            services.AddSingleton(typeof(IUnitOfWork<>), typeof(IUnitOfWork<>));
            
            return services;
        }

and startup
public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //настройка сервисов
          
            ConfigService.InitServices(services, Configuration);

            services.AddControllersWithViews();
            services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/build"; });
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-06-10
@DD-var

Need to create a DesignTimeContextFactory https://go.microsoft.com/fwlink/?linkid=851728

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question