Michael Sync

Michael Sync

Crisp, minimal personal blog

05 Nov 2013

“The Provider encountered an unknown error” when creating new users in EF DbMigrationsConfiguration.Seed method


The objective was to create some default users in EF Seed method so we could login without having a link for new user registration in application.

The code that we have was as below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
WebSecurity.InitializeDatabaseConnection("DbContext", "UserAccounts", "Id", "UserLogOnId", true);

if (!WebSecurity.UserExists("Test1")) {
  WebSecurity.CreateAccount("Test1", "123456", false);
}

context.UserAccounts.AddOrUpdate(
    u => u.UserLogOnId,
    new UserAccount {
      UserLogOnId = "Test1",
        UserDisplayName = "Test Staff 1",
        Status = UserAccountStatus.Enabled,
        ModifiedBy = string.Empty,
        EmailAddress = "user1@gmail.com",
        LastModifiedDate = new DateTimeOffset(DateTime.UtcNow, TimeSpan.Zero),
        LastLogOnDateTime = new DateTimeOffset(DateTime.UtcNow, TimeSpan.Zero)
    },

When we called update-database from nuget console, we got this error below. It was a bit weird because we already had  all required assemblies referenced in the project and we had the correct configuration in app.config as well. Why did we get this error?

Error Message

Running Seed method.

System.Web.Security.MembershipCreateUserException: The Provider encountered an unknown error. at WebMatrix.WebData.SimpleMembershipProvider.CreateAccount(String userName, String password, Boolean requireConfirmationToken) at WebMatrix.WebData.WebSecurity.CreateAccount(String userName, String password, Boolean requireConfirmationToken) at MyProject.Migrations.Configuration.Seed(MyDbContext context) in »c:\Users\root\Desktop\MyProject\codes\src\Models\Migrations\Configuration.cs:line 39 at System.Data.Entity.Migrations.DbMigrationsConfiguration`1.OnSeed(DbContext context) at System.Data.Entity.Migrations.DbMigrator.SeedDatabase() at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.SeedDatabase() at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.b__b() at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration) at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run() at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate) at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate) at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner) at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force) at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.b__0() at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)

The Provider encountered an unknown error

EF Seed and WebSecurity - 1

Solution

Yes. The error message might be a bit misleading. But we realized about the problem after looking at the API below. You can’t create the account without creating the user. That’s why we got this error above. Its nothing related to the provider.

Here, as you can see,  this API below allows us to create both user and account at the same time and you can pass the custom properties that you can for user table.

WebSecurity.CreateUserAndAccount

Here is the correct code that you should use if you want to create a default user from Seed method. Of course, don’t forget to add the required assemblies as references in your startup project.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
WebSecurity.InitializeDatabaseConnection("DbContext", "UserAccounts", "Id", "UserLogOnId", true);

if (!WebSecurity.UserExists("Test1")) {
  WebSecurity.CreateAccount("Test1", "123456",
    new {
      UserDisplayName = "Test Staff 1",
        Status = UserAccountStatus.Enabled,
        ModifiedBy = string.Empty,
        EmailAddress = "user1@gmail.com",
        LastModifiedDate = new DateTimeOffset(DateTime.UtcNow, TimeSpan.Zero),
        LastLogOnDateTime = new DateTimeOffset(DateTime.UtcNow, TimeSpan.Zero)
    }
  );
}

Hope it helps.