0

In relation to this question: MVC 5 Seed Users and Roles I tried to set up a default user seed. The way I tried it in the Configuration.cs was:

var passwordHash = new PasswordHasher();
string defaultPassword = passwordHash.HashPassword("SuperPassword");

context.Users.AddOrUpdate(u => u.UserName,
   new ApplicationUser
   {
       UserName = "admin@project.loc",
       Email = "admin@project.loc",
       PasswordHash = defaultPassword
   });

But now I have the problem that I can't use the commands to migrate the database. Solutions that worked for other people unfortuantely didn't help me.

When I try to update-database I'm getting the error:

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

I tried to use Add-Migration <name> -IgnoreChanges but got the error, that migrations are still pending.

Earlier I tried to enable-migrations for my both contexts but for one I got this error message:

Creating a DbModelBuilder or writing the EDMX from a DbContext created using Database First or Model First is not supported. EDMX can only be obtained from a Code First DbContext created without using an existing DbCompiledModel.

Enable-Migration for the other context just overwrote my Configuration.cs with the customized seed.

Community
  • 1
  • 1
Zumarta
  • 145
  • 3
  • 18
  • You can add a migration with empty `Up` and `Down` methods. Sometimes EF gets out of sync with the database, and needs a 'sync' migration to get back in line. That is: run `Add-Migration sync`, then manually review and delete already-existing schema changes in the up/down methods (even if it means making the methods do nothing). – Rob Dec 11 '15 at 08:13
  • I'll try that but how can I cancel pending migrations? There is now shortcut like Ctrl + C, isn't it? – Zumarta Dec 11 '15 at 08:24
  • if by cancel them, you mean get rid of them entirely; you merely have to delete the migration file(s). If by cancel you mean "don't run this time", I don't think that's possible - atleast not without actively fighting against EF (which I don't recommend) – Rob Dec 11 '15 at 08:25
  • That being said, when you delete the migration file and run `Add-Migration` again, the deleted changes will also appear in the new migration file (which you can remove/edit manually) – Rob Dec 11 '15 at 08:27

1 Answers1

0

The tip with editing the Up and Down methods helped me, thank you! Didn't know that I can edit the operations here.

Zumarta
  • 145
  • 3
  • 18