2

I'm quite new to Entity Framework Core, and mostly I learn by myself from MSDN website. Currently I'm learning Identity

Though I'm using dotnet core 3.0, but current project is using dotnet core 2.2, so I choose 2.2 as a start point of Identity learning.

Since I want to customize Identity, I used the following step to start this new project:

  1. Create a new project, choose "ASP.NET Core Web Application", ".NET Core", "ASP.NET Core 2.2", "Web Application (Model-View-Controller), change "Authentication", using "Individual User Accounts"

  2. Update "appsettings.json" with external database "ConnectionStrings" content

  3. In PMC, use command "Update-Database" to update database
  4. Database and all Identity tables are created, project is run correctly
  5. add migration to update table and column through command "Add-migration 1stmigration"
  6. Update Up() and Down() functions:
    public partial class _1stmigration : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.RenameTable(
                name: "AspNetUsers",
                newName: "Users");
            migrationBuilder.RenameColumn(
                name: "Email", 
                table: "Users",
                newName: "EmailAddress");
            migrationBuilder.DropIndex(
                name: "UserNameIndex",
                table: "Users");
            migrationBuilder.DropColumn(
                name: "NormalizedUserName",
                table: "Users");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<string>(
                name: "NormalizedUserName",
                table: "Users");
            migrationBuilder.CreateIndex(
                name: "UserNameIndex",
                table: "Users",
                column: "NormalizedUserName");
            migrationBuilder.RenameColumn(
                name: "EmailAddress", 
                table: "Users",
                newName: "Email");
            migrationBuilder.RenameTable(
                name: "Users",
                newName: "AspNetUsers");
        }
    }

Code is to change default "AspNetUsers" table name to "Users", and change "Email" column to "EmailAddress", and delete "NormalizedUserName" column and "UserNameIndex" index. 6. run command "Update-Database" to update Database table. Database table/column are updated as what's needed, but when running the project to register a new user, it returns error:

SqlException: Invalid object name 'AspNetUsers'.

System.Data.SqlClient.SqlCommand+<>c.b__122_0(Task result)

I checked the snapshot file in "Migrations" folder, finding the code creating database table is not changed. My question is: 1. When the snapshot file "ApplicationDbContextModelSnapshot.cs" is updated, I thought it should be updated when executing "Add-migration" command after Down() and Up() functions have the updated code 2. How to make it work till the previous steps are done?

Thanks for the help.

R. H.
  • 23
  • 4
  • it upgrades when you update your database, it's basically a snapshot of your current database schema. – KiKoS Dec 03 '19 at 22:15

1 Answers1

0
  1. The ApplicationDbContextModelSnapshot upgrades when you add a migration, after changing your DbContext, in a way it reflects the actual schema of your database.

  2. Your initial migration is initializing identity default tables, i don't recommend modifying it, if you wish to change something in those default tables you could add a second empty migration Add-Migration and modify that one but i would not recommend that either, instead you could change that in your database context, More in here.

Edit: Corrected my answer and made it clearer

KiKoS
  • 428
  • 1
  • 7
  • 18
  • Thanks for the quick answer, although you recommend me not to do so, I just want to under how the snapshot file is updated, so I added a second migration, and update database, the snapshot file content is still unchanged. It seems it is not reflecting the up-to-date database schema. That confused me. – R. H. Dec 03 '19 at 22:39
  • 1
    That's because your DbContext is supposed to be in sync with your database schema, technically the DbContextModelSnapshot is also the current state of your DbContext, making changes in a migration would not change your DbContext so the snapshot won't change also. – KiKoS Dec 03 '19 at 23:02
  • Thanks for the help and the above link. – R. H. Dec 04 '19 at 14:15
  • Glad i could help. – KiKoS Dec 04 '19 at 15:37