Question

Previously I used a database initializer to seed or pre-populate my tables when creating them using Code First. This all worked great but I wanted to move on to using the EF 4.3 migrations.

I updated to EF 4.3 in my project and removed the database initializer code and put it within the Seed method on the migrations Configuration.cs file. This is all working ok for me for prepopulating my tables except for when I want to seed a default user into my user table.

I use the following code within my Seed method: (currently this doesn't use the AddOrUpdate code as I'm not sure how to use that for this situation - hence the username check)

        // create default User
        MembershipCreateStatus status = new MembershipCreateStatus();
        User admin = context.Users.Find("TestGuy");
        if (admin == null)
        {
            Membership.CreateUser("TestGuy", "TestGuy123", "testguy@test.com");
            if (status == MembershipCreateStatus.Success)
            {
                admin.Firstname = "Test";
                admin.Surname = "Guy";
                admin.DateLastActivity = DateTime.Now;
                admin.LastActivityRoute = "/Home/Index";
                Role adminRole = context.Roles.Find("Administrator");
                admin.Roles = new List<Role>();
                admin.Roles.Add(adminRole);
            }
        }

When I try and run Update-Database I get this error:

The password-answer supplied is invalid.

I think this is just down to how I call the CreateUser but I can't seem to work out how to get around this bug. I've tried putting in just nulls for the security question and answer but that doesn't seem to work.

Has anyone an example of seeding a user using the 'AddOrUpdate' option?

EDIT 1:

As per one of the comments below, I am using Altairis Security Toolkit to manage my membership. My web.config is setup as follows:

<membership defaultProvider="TableMembershipProvider">
  <providers>
    <clear />
    <add name="TableMembershipProvider" type="Altairis.Web.Security.TableMembershipProvider, Altairis.Web.Security" connectionStringName="DataContext" minRequiredPasswordLength="8" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" minRequiredNonAlphanumericCharacters="0" passwordStrengthRegularExpression="" />
  </providers>
</membership>

EDIT 2:

I have also put this as an open ticket on the Altairis forum. http://altairiswebsecurity.codeplex.com/workitem/32273

EDIT 3:

To get this working without any error I had to setup my code as follows:

        // create default User
        MembershipCreateStatus status = new MembershipCreateStatus();
        User admin = context.Users.Find("TestGuy");
        if (admin == null)
        {
            Membership.CreateUser("TestGuy", "TestGuy123", "testguy@test.com", null, null, true, out status);
            if (status == MembershipCreateStatus.Success)
            {
                admin = context.Users.Find("TestGuy");
                admin.Firstname = "Test";
                admin.Surname = "Guy";
                admin.DateLastActivity = DateTime.Now;
                admin.LastActivityRoute = "/Home/Index";
                Role adminRole = context.Roles.Find("Administrator");
                admin.Roles = new List<Role>();
                admin.Roles.Add(adminRole);
            }
        }

The above will compile now when running Update-Database but no new user is generated. If anyone has suggestions to try that would be great!

Thanks,

Rich

Was it helpful?

Solution 2

From doing some research on this issue it's become apparent that the user seeding should be created outside the migrations seed logic - at least for now until they make it easier to do!

As a work around I followed the example from this Stackoverflow question (Best way to incrementally seed data in Entity Framework 4.3) to manually make my new user on start up outside of the migrations configuration file.

It's not ideal, at least to me, as all 'startup' data is not in one area but it gets the job done and isn't that difficult to follow.

OTHER TIPS

I think your Membership is set to require question and answer. Have you tried putting a check in using "if (Membership.RequiresQuestionAndAnswer)" and see which way it branches.

To Admin: By the way I wanted to post this first as a comment as I was flagged last time when using an answer, but I don't see any option here for adding comments, only answers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top