none
Get aspnet membership/role working with DropCreateDatabaseAlways RRS feed

  • Question

  • Using MVC3 & EF.  I want EF to recreate the database, but leave alone the membership stuff. At the moment, I want to recreate the DB and seed the data so I start from a consistent known data point. How can I configure the DropCreateDatabaseAlways to ignore the membership tables (and maybe stored procedures if they are removed too)?

    Am happy for it to drop the membership stuff and recreate it - not to hard to code adding the few uers, roles and permissions we have setup.

    Thanks

     


    • Edited by OblivionSY Tuesday, October 25, 2011 9:27 AM
    Tuesday, October 25, 2011 9:26 AM

Answers

  • here was my solution:

     InstallMembershipScripts(); was called nmear the top of the seeding process, followed by Membership code to create rolls, and users etc. The membership files (found in the .NET Framework folder were added to the project. The code should explain what is happening.

    #region Membership Stuff
            private void InstallMembershipScripts()
            {
                //get the script
                SqlConnection con = new SqlConnection();
                con.ConnectionString =
                    ConfigurationManager.ConnectionStrings[LS.Constants.Database.ConnectionStringName].ConnectionString;
    
                InstallScript("InstallCommon.sql", con);
                InstallScript("InstallMembership.sql", con);
                InstallScript("InstallRoles.sql", con);
                InstallScript("InstallProfile.sql", con);
            }
    
            private void InstallScript(string script, SqlConnection con)
            {
                string scriptText = GetScript(script);
                string[] commandTexts = GetScriptSections(scriptText);
                Array.ForEach(commandTexts, s => RunSection(s, con));
            }
    
            private string GetScript(string scriptName)
            {
                using (var text = File.OpenText(HttpContext.Current.Server.MapPath("~/Infrastructure/Database/" + scriptName)))
                {
                    var script = text.ReadToEnd();
                    script = script.Replace("aspnetdb", "dbNameHere");
                    text.Close();
                    return script;
                }
    
            }
    
            private string[] GetScriptSections(string scriptText)
            {
                //split the script on "GO" commands
                string[] splitter = new string[] { "\r\nGO\r\n" };
                string[] commandTexts = scriptText.Split(splitter,
                  StringSplitOptions.RemoveEmptyEntries);
    
                return commandTexts;
            }
    
            private void RunSection(string commandText, SqlConnection con)
            {
                if (con.State == ConnectionState.Closed)
                    con.Open();
                using (var command = new SqlCommand(commandText, con))
                {
                    command.ExecuteNonQuery();
                }
            }
        }
            #endregion
    

    • Marked as answer by OblivionSY Monday, October 31, 2011 2:29 AM
    Monday, October 31, 2011 2:29 AM

All replies

  • Hello,

    you cannot because it Drops the database (not only selected tables) and all its content - all built-in initializers drop the database during recreation. You can create your own derived database initializer and execute membership tables creation in overriden Seed method.

    Best regards,
    Ladislav

    Tuesday, October 25, 2011 11:27 AM
  • Ok, so how can I install Membership back into the database automatically? It dropping is not necersarily an issue (might want to clear out registered users and revert back to default list) but in the seed method, will need to install the memebership tables again. Is this possible?

    Thanks

    Tuesday, October 25, 2011 11:37 AM
  • You will take a scripts used to create tables, relations, etc. for Membership and execute them in Seed method as normal SQL. Be aware that you cannot execute whole script provide by ASP.NET because it uses GO statements. To execute script with GO statements you must use SQL Server Management objects instead of common database access (ADO.NET or context.Database.ExecuteSqlCommand).

    Best regards,
    Ladislav

    Wednesday, October 26, 2011 8:57 AM
  • Hi,

    I am writing to check the status of the issue on your side.Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know.I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 31, 2011 2:14 AM
    Moderator
  • here was my solution:

     InstallMembershipScripts(); was called nmear the top of the seeding process, followed by Membership code to create rolls, and users etc. The membership files (found in the .NET Framework folder were added to the project. The code should explain what is happening.

    #region Membership Stuff
            private void InstallMembershipScripts()
            {
                //get the script
                SqlConnection con = new SqlConnection();
                con.ConnectionString =
                    ConfigurationManager.ConnectionStrings[LS.Constants.Database.ConnectionStringName].ConnectionString;
    
                InstallScript("InstallCommon.sql", con);
                InstallScript("InstallMembership.sql", con);
                InstallScript("InstallRoles.sql", con);
                InstallScript("InstallProfile.sql", con);
            }
    
            private void InstallScript(string script, SqlConnection con)
            {
                string scriptText = GetScript(script);
                string[] commandTexts = GetScriptSections(scriptText);
                Array.ForEach(commandTexts, s => RunSection(s, con));
            }
    
            private string GetScript(string scriptName)
            {
                using (var text = File.OpenText(HttpContext.Current.Server.MapPath("~/Infrastructure/Database/" + scriptName)))
                {
                    var script = text.ReadToEnd();
                    script = script.Replace("aspnetdb", "dbNameHere");
                    text.Close();
                    return script;
                }
    
            }
    
            private string[] GetScriptSections(string scriptText)
            {
                //split the script on "GO" commands
                string[] splitter = new string[] { "\r\nGO\r\n" };
                string[] commandTexts = scriptText.Split(splitter,
                  StringSplitOptions.RemoveEmptyEntries);
    
                return commandTexts;
            }
    
            private void RunSection(string commandText, SqlConnection con)
            {
                if (con.State == ConnectionState.Closed)
                    con.Open();
                using (var command = new SqlCommand(commandText, con))
                {
                    command.ExecuteNonQuery();
                }
            }
        }
            #endregion
    

    • Marked as answer by OblivionSY Monday, October 31, 2011 2:29 AM
    Monday, October 31, 2011 2:29 AM