8

I was wondering what would be the best way to install the membership schema on the fly.

Right now the solution I'm thinking of is to somehow run aspnet_regsql.exe with arguments to execute on my db connection.

How can I accomplish this? Is there a better way?

2 Answers 2

8

Ok I found a much better way to accomplish this, I still use the MembershipExists() function, but I install it with this statement, which I didn't know of up until a couple of minutes ago.

SqlServices.Install(database, SqlFeatures.All, connectionString);
Sign up to request clarification or add additional context in comments.

Comments

2

Did it like this:

    public static void Initialize()
    {
        var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["membership"].ConnectionString);

        if (!MembershipExists(connection))
        {
            // create schema
            string regsql = Path.Combine(System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(), "aspnet_regsql.exe");
            string args = string.Format(@"-E -S {0} -A all -d {1}", connection.DataSource, connection.Database);

            var proc = Process.Start(regsql, args);
            if (proc != null)
                proc.WaitForExit();
        }
    }

    public static bool MembershipExists(SqlConnection connection)
    {
        try
        {
            connection.Open();
            var query = new SqlCommand("select count(*) from sysobjects where name = 'aspnet_CheckSchemaVersion' and type = 'P'", connection);
            return query.ExecuteScalar() as int? == 1;
        }
        finally
        {
            connection.Close();
        }
    }

Not sure how it'll behave on a production environment.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.