summaryrefslogtreecommitdiffstats
path: root/projecttemplates/RelyingPartyLogic/Utilities.cs
diff options
context:
space:
mode:
Diffstat (limited to 'projecttemplates/RelyingPartyLogic/Utilities.cs')
-rw-r--r--projecttemplates/RelyingPartyLogic/Utilities.cs54
1 files changed, 43 insertions, 11 deletions
diff --git a/projecttemplates/RelyingPartyLogic/Utilities.cs b/projecttemplates/RelyingPartyLogic/Utilities.cs
index fb05306..bbcfc68 100644
--- a/projecttemplates/RelyingPartyLogic/Utilities.cs
+++ b/projecttemplates/RelyingPartyLogic/Utilities.cs
@@ -11,6 +11,7 @@ namespace RelyingPartyLogic {
using System.Data.Common;
using System.Data.EntityClient;
using System.Data.Objects;
+ using System.Data.SqlClient;
using System.Globalization;
using System.IO;
using System.Linq;
@@ -40,27 +41,57 @@ namespace RelyingPartyLogic {
public static void CreateDatabase(Identifier claimedId, string friendlyId, string databaseName) {
const string SqlFormat = @"
-CREATE DATABASE [{0}] ON (NAME='{0}', FILENAME='{0}')
+{0}
GO
-USE ""{0}""
-GO
-{1}
-EXEC [dbo].[AddUser] 'admin', 'admin', '{2}', '{3}'
+EXEC [dbo].[AddUser] 'admin', 'admin', '{1}', '{2}'
GO
";
- string schemaSql;
+ var removeSnippets = new string[] { @"
+IF EXISTS (SELECT 1
+ FROM [master].[dbo].[sysdatabases]
+ WHERE [name] = N'$(DatabaseName)')
+ BEGIN
+ ALTER DATABASE [$(DatabaseName)]
+ SET HONOR_BROKER_PRIORITY OFF
+ WITH ROLLBACK IMMEDIATE;
+ END
+
+
+GO", @"
+PRINT N'Creating AutoCreatedLocal...';
+
+
+GO
+CREATE ROUTE [AutoCreatedLocal]
+ AUTHORIZATION [dbo]
+ WITH ADDRESS = N'LOCAL';
+
+
+GO
+" };
+ string databasePath = HttpContext.Current.Server.MapPath("~/App_Data/" + databaseName + ".mdf");
+ StringBuilder schemaSqlBuilder = new StringBuilder();
using (var sr = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(DefaultNamespace + ".CreateDatabase.sql"))) {
- schemaSql = sr.ReadToEnd();
+ schemaSqlBuilder.Append(sr.ReadToEnd());
}
- string databasePath = HttpContext.Current.Server.MapPath("~/App_Data/" + databaseName + ".mdf");
- string sql = string.Format(CultureInfo.InvariantCulture, SqlFormat, databasePath, schemaSql, claimedId, "Admin");
+ foreach (string remove in removeSnippets) {
+ schemaSqlBuilder.Replace(remove, string.Empty);
+ }
+ schemaSqlBuilder.Replace("$(Path1)", HttpContext.Current.Server.MapPath("~/App_Data/"));
+ schemaSqlBuilder.Replace("WEBROOT", databasePath);
+ schemaSqlBuilder.Replace("$(DatabaseName)", databaseName);
+
+ string sql = string.Format(CultureInfo.InvariantCulture, SqlFormat, schemaSqlBuilder, claimedId, "Admin");
var serverConnection = new ServerConnection(".\\sqlexpress");
try {
serverConnection.ExecuteNonQuery(sql);
- var server = new Server(serverConnection);
- server.DetachDatabase(databasePath, true);
} finally {
+ try {
+ var server = new Server(serverConnection);
+ server.DetachDatabase(databaseName, true);
+ } catch (FailedOperationException) {
+ }
serverConnection.Disconnect();
}
}
@@ -79,6 +110,7 @@ GO
}
DbCommand cmd = connection.CreateCommand();
+ cmd.Transaction = (DbTransaction)Database.DataContextTransaction;
cmd.CommandText = command;
cmd.CommandType = CommandType.StoredProcedure;
try {