Register User never makes it into the database

May 21, 2012 at 5:39 PM

 

When registering a new user, my new user gets created successfully through Membership.CreateUser however when the DbContext is created and the firstOrDefaults takes place EF always returns NULL, which then causes a crash at FirstName.  The really odd thing is that after the failure, you can double back and logon with that new Username and Password successfully

public static MembershipCreateStatus Register(string Username, string Password, string Email, bool IsApproved, string FirstName, string LastName, int ZipCode)
        {
            MembershipCreateStatus CreateStatus;
            Membership.CreateUser(Username, Password, Email, null, null, IsApproved, null, out CreateStatus);

            if (CreateStatus == MembershipCreateStatus.Success)
            {
                using (DataContext Context = new DataContext())
                {
                    User User = Context.Users.FirstOrDefault(Usr => Usr.Username == Username);
                    User.FirstName = FirstName;

Right now I have a SQLExpress connection string
    <add name="DataContext" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />

With all the services pointing back to DataContext

 <membership>
      <providers>
        <clear />
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="DataContext" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
      </providers>
    </membership>
    <profile>
      <providers>
        <clear />
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="DataContext" applicationName="/" />
      </providers>
    </profile>
    <roleManager enabled="false">
      <providers>
        <clear />
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="DataContext" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>
Membership.config also has a duplicate of this connection string with no clear <connectionStrings>

    <!--Clear any connection strings to use the default SQL Express, or name your connection string "DataContext" and point to anywhere-->
    <add name="DataContext" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
  </connectionStrings>

Browsing to the database I have all the tables and fields but no entries

Help! :)

 

Coordinator
May 22, 2012 at 4:59 PM

You are using it the wrong way, delete the connection string and it will generate the database automatically.

If you want to use the connection string, then you do not attach a database.

You need to read more on how Entity Framework Code-First works.

http://msdn.microsoft.com/en-us/library/gg696189(v=VS.103).aspx