AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

How to generate migrations for this new ApiAuthorizationDbContext?

Open dotnicht opened this issue 7 years ago • 4 comments

Since I can't get access to IOptions<OperationalStoreOptions> from IDesignTimeDbContextFactory


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

dotnicht avatar Aug 16 '19 19:08 dotnicht

There's nothing special to using ApiAuthorizationDbContext. IDesignTimeDbContextFactory. Create a service collection. Initialize your container as you would do in your app. Resolve the required services.

Alternatively, simply avoid using the class, and have your DbContext extend IdentityDbContext and implement the appropriate interfaces as https://github.com/aspnet/AspNetCore/blob/master/src/Identity/ApiAuthorization.IdentityServer/src/Data/ApiAuthorizationDbContext.cs does.

javiercn avatar Aug 17 '19 10:08 javiercn

i guess it was my local issue with tooling version. thx!

dotnicht avatar Aug 17 '19 12:08 dotnicht

It look like the last time it worked for me was 3.0.0-preview7.19362.6. I get an object reference error on the following:

private static Assembly GetApplicationAssembly(IdentityBuilder builder)
        {
            // Whis is the same logic that MVC follows to find the application assembly.
            var environment = builder.Services.Where(d => d.ServiceType == typeof(IWebHostEnvironment)).ToArray();
            var applicationName = ((IWebHostEnvironment)environment.LastOrDefault()?.ImplementationInstance)
                .ApplicationName;

Below is my design code. My AuthDbContext is extracted out into a library project.

 public class DesignTimeAuthDbContextFactory : IDesignTimeDbContextFactory<AuthDbContext>
    {
        private const string CONN_STRING = "Server=(local);Database=MyDb;Trusted_Connection=True;MultipleActiveResultSets=true";
        public AuthDbContext CreateDbContext(string[] args)
        {
            Debugger.Launch();
            IServiceCollection services = new ServiceCollection();

            var cAuth = new AuthConfiguration()
            {
                ConnectionString = CONN_STRING
            };

            services.AddSingleton<AuthConfiguration>(cAuth);
            services.AddDbContext<AuthDbContext>(options => options.UseSqlServer(cAuth.ConnectionString, x => x.MigrationsHistoryTable(HistoryRepository.DefaultTableName, AuthConfiguration._SCHEMA)));

            services
                .AddDefaultIdentity<ApplicationUser>()
                .AddRoles<IdentityRole>()
                .AddEntityFrameworkStores<AuthDbContext>();

            services.AddIdentityServer()
                .AddApiAuthorization<ApplicationUser, AuthDbContext>();

            var context = services.BuildServiceProvider().GetService<AuthDbContext>();

            return context;
        }

sikemullivan avatar Dec 10 '19 05:12 sikemullivan

Fixed

  public class DesignTimeAuthDbContextFactory : IDesignTimeDbContextFactory<AuthDbContext>
    {
        private const string CONN_STRING = "Server=(local);Database=MyDb;Trusted_Connection=True;MultipleActiveResultSets=true";
        public AuthDbContext CreateDbContext(string[] args)
        {
            IServiceCollection services = new ServiceCollection();


            services.AddDbContext<AuthDbContext>(options => options.UseSqlServer(CONN_STRING));
            services.Configure<IdentityServer4.EntityFramework.Options.OperationalStoreOptions>(x =>{});

            var context = services.BuildServiceProvider().GetService<AuthDbContext>();

            return context;
        }

    }

sikemullivan avatar Dec 10 '19 05:12 sikemullivan