5 Replies - 329 Views - Last Post: 29 March 2019 - 02:42 AM Rate Topic: -----

#1 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6795
  • View blog
  • Posts: 28,082
  • Joined: 12-December 12

[Core] create Identity tables in existing database

Posted 28 March 2019 - 08:59 AM

I have an ASP.NET Core application and added scaffolding for the default Identity configuration. This creates an Areas folder, Identity folder, with IdentityHostingStartup.cs. It looks like the following.

[assembly: HostingStartup(typeof(People.Areas.Identity.IdentityHostingStartup))]
namespace People.Areas.Identity
{
    public class IdentityHostingStartup : IHostingStartup
    {
        public void Configure(IWebHostBuilder builder)
        {
            builder.ConfigureServices((context, services) => {
                services.AddDbContext<IdentityContext>(options =>
                    options.UseSqlServer(
                        context.Configuration.GetConnectionString("IdentityConnection")));

                //services.AddDefaultIdentity<User>()
                //    .AddEntityFrameworkStores<IdentityContext>();
            });
        }
    }
}


I've had this application for a while and I previously managed to run it so that it created the needed Identity tables (dbo.AspNetRoles, etc.) in a separate database I created named People. I never cottoned on as to how I achieved this, I just suspected that it happened because the database was empty(?).

I am creating/copying a new version of this application and I want it to create these Identity tables in my existing (application) database. Let's call it Tiddles.

In appsettings.json I have two connection strings configured, IdentityConnection and DefaultConnection, both pointing to the existing application database.

On running this new application it is not creating the Identity tables. How can I persuade it to do this? I am not sure where to look for some magic keyword or bit of code that would achieve this.

Here is some relevant code from Startup.cs if it helps:

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

        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)
        {
            services.AddMvc(config =>
            {
                // using Microsoft.AspNetCore.Mvc.Authorization;
                // using Microsoft.AspNetCore.Authorization;
                var policy = new AuthorizationPolicyBuilder()
                                 .RequireAuthenticatedUser()
                                 .Build();
                config.Filters.Add(new AuthorizeFilter(policy));
            })
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            // In production, the Angular files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/dist";
            });

            services.AddDbContext<PeopleContext>(options
                => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddDefaultIdentity<User>().AddRoles<IdentityRole>()
                .AddEntityFrameworkStores<IdentityContext>();
        }

Thank you for your advice.

Is This A Good Question/Topic? 0
  • +

Replies To: [Core] create Identity tables in existing database

#2 DarenR   User is online

  • D.I.C Lover

Reputation: 612
  • View blog
  • Posts: 4,042
  • Joined: 12-January 10

Re: [Core] create Identity tables in existing database

Posted 28 March 2019 - 11:45 AM

are you setting your initial catalog in your connection string?

Data Source=.\SQLEXPRESS; Initial Catalog=MyDatabase;


and i am assuming you have an using in your json that created the table using the constring?
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6795
  • View blog
  • Posts: 28,082
  • Joined: 12-December 12

Re: [Core] create Identity tables in existing database

Posted 28 March 2019 - 11:56 AM

Hello.

No longer at work today.

Yes, I have an initial catalog. I haven't seen or created any obvious code that says "If the needed tables aren't present create them". Although, this did happen automatically with the earlier incarnation.
Was This Post Helpful? 0
  • +
  • -

#4 smendoza88   User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 112
  • Joined: 13-July 11

Re: [Core] create Identity tables in existing database

Posted 28 March 2019 - 01:52 PM

I would say you the keywords would be scaffolding and migrations.
Check this article https://docs.microso...s=visual-studio
Was This Post Helpful? 1
  • +
  • -

#5 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6795
  • View blog
  • Posts: 28,082
  • Joined: 12-December 12

Re: [Core] create Identity tables in existing database

Posted 29 March 2019 - 02:27 AM

I have the initial migration that I can see would create the tables I need. My concern is that I cannot see where the (new) connection is applied, so that I suspect that it would create a localDb as it did originally.

I think I could do PM > Update-Database to execute it. reference

Any further advice/thoughts? I wanted to delay trying this until I had further confirmation.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6795
  • View blog
  • Posts: 28,082
  • Joined: 12-December 12

Re: [Core] create Identity tables in existing database

Posted 29 March 2019 - 02:42 AM

Found it! :bananaman:

PM> Update-Database -Context IdentityContext


(I had actually executed this at one point previously, but lost track of it.)

IdentityContext is configured (in IdentityHostingStartup) to refer to my connection string "IdentityConnection", which points to my application database. Now the required Identity tables are created in my database, rather than a separate one or localDb.



I am curious, though, on technically how the IdentityContext is known when executing Update-Database from the Package Manager Console? It is configured in a .cs file... how does the package manager know about this C# file?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1