2

I have connected to an existing SQL database that didn't have any identity, eg no aspNetUser table etc. I have since added these using a custom login class like this

public class UserLogin : IdentityUser
{       
    public int LanguageId { get; set; }
    public string HQ_PRM_Id { get; set; }       
}

I then generated a migration to create the tables like this (I just called it identity)

"dotnet ef migrations add Identity"

I then ran the migration using

"dotnet ef database update Identity"

That created the tables so all good. However, I forgot to add a firstname and lastname to the user class which I have since added. The problem is when I run my new migration it says

There is already an object named 'AspNetRoles' in the database.

How do I tell the migration to update the schema and not try to recreate the tables? Otherwise I have to manually delete the objects first.. which means losing data.

Thanks

6
  • Update the object with the new properties and create a new migration. this will detect that the table already exists and create a script that only updates the existing table Commented Nov 13, 2019 at 9:49
  • Unfortunately it doesn't do that, the generated migration code recognises the additional columns but tries to create the table again, not update. Commented Nov 13, 2019 at 9:55
  • How did you create the second migration? Commented Nov 13, 2019 at 9:57
  • The same as the last time, using "dotnet ef migrations add Identity" Commented Nov 13, 2019 at 9:59
  • You want to create the second migration with a different name Commented Nov 13, 2019 at 10:08

1 Answer 1

5

During development models frequently change and these models (which represent database tables) get out of sync with the tables themselves. The role of migrations is to keep the database and the models in sync and therefore whenever you make a change to the model you must create a new migration to incrementally update the database schema.

Initial migration

public class UserLogin : IdentityUser
{       
    public int LanguageId { get; set; }
    public string HQ_PRM_Id { get; set; }       
}

dotnet ef migrations add Identity - This command creates a new migration called Identity

dotnet ef database update Identity - This command runs migrations upto and including one with the name "Identity"

Second migration

public class UserLogin : IdentityUser
{       
    public int LanguageId { get; set; }
    public string HQ_PRM_Id { get; set; }
    public string FirstName { get; set; }       
    public string LastName { get; set; }            
}

dotnet ef migrations add AddNames - This command creates a new migration called AddNames which EF should recognise is an update to the exsiting tables (and therefore the migration should only be and update)

dotnet ef database update AddNames - This will then run the AddNames migration

I'd definitely recommend checking the MS docs on the subject

Sign up to request clarification or add additional context in comments.

4 Comments

Even when adding a new migration with a different name it still tries to recreate other tables. For example the new migration file only refers the column I have added, but the ContextModelSnapshot.cs still refers to the other tables as usual. and I get an "object already exisits" for aspNetRoles which has not changed..
The ContextModelSnapshot class is the current state of the model. The file is added to the Migrations folder when the first migration is created, and updated with each subsequent migration. It enables the migrations framework to calculate the changes required to bring the database up to date with the model. e.g when creating your new migration it will check the snapshot to see the current state of the database, discover that the table already exists, and create a migration which updates the existing table. It sounds like your ContextModelSnapshot.cs is doing exactly what it's intended to do
Again, that information is in the MS Docs What command are you running to run your migration? Have you checked which migrations have been run against your database in th __EFMigrations table?
So I the __EFMigrationsHistory table shows the previous migration called Identity. That was the initial one. Then I added a column and created a migration called AddPasswordChangeColumn. The 20191114125408_AddPasswordChangeColumn.cs file has the UP and Down methods, the Up method only adds the column which is great. But when I update against it then it still attempts to create another table which hasnt changed since the Identity migration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.