diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2010-01-01 19:04:55 -0800 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2010-01-01 19:19:50 -0800 |
commit | edaea39fc722fea78c115e05726ad6e0447ae89b (patch) | |
tree | d0e2597085c00d7047bb1bdd9eeb554e98252bb5 /projecttemplates/RelyingPartyLogic/Utilities.cs | |
parent | a72bcc6f5bfc036e294530cf3067667a3a6c6efd (diff) | |
download | DotNetOpenAuth-edaea39fc722fea78c115e05726ad6e0447ae89b.zip DotNetOpenAuth-edaea39fc722fea78c115e05726ad6e0447ae89b.tar.gz DotNetOpenAuth-edaea39fc722fea78c115e05726ad6e0447ae89b.tar.bz2 |
Fixed intermittent error while executing SQL stored procedures.
Diffstat (limited to 'projecttemplates/RelyingPartyLogic/Utilities.cs')
-rw-r--r-- | projecttemplates/RelyingPartyLogic/Utilities.cs | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/projecttemplates/RelyingPartyLogic/Utilities.cs b/projecttemplates/RelyingPartyLogic/Utilities.cs index bbcfc68..6552596 100644 --- a/projecttemplates/RelyingPartyLogic/Utilities.cs +++ b/projecttemplates/RelyingPartyLogic/Utilities.cs @@ -96,21 +96,38 @@ GO } } + public static int ExecuteCommand(this ObjectContext objectContext, string command) { + // Try to automatically add the appropriate transaction if one is known. + EntityTransaction transaction = null; + if (Database.IsDataContextInitialized && Database.DataContext == objectContext) { + transaction = Database.DataContextTransaction; + } + return ExecuteCommand(objectContext, transaction, command); + } + /// <summary> /// Executes a SQL command against the SQL connection. /// </summary> /// <param name="objectContext">The object context.</param> + /// <param name="transaction">The transaction to use, if any.</param> /// <param name="command">The command to execute.</param> /// <returns>The result of executing the command.</returns> - public static int ExecuteCommand(this ObjectContext objectContext, string command) { - DbConnection connection = ((EntityConnection)objectContext.Connection).StoreConnection; + public static int ExecuteCommand(this ObjectContext objectContext, EntityTransaction transaction, string command) { + if (objectContext == null) { + throw new ArgumentNullException("objectContext"); + } + if (String.IsNullOrEmpty(command)) { + throw new ArgumentNullException("command"); + } + + DbConnection connection = (EntityConnection)objectContext.Connection; bool opening = (connection.State == ConnectionState.Closed); if (opening) { connection.Open(); } DbCommand cmd = connection.CreateCommand(); - cmd.Transaction = (DbTransaction)Database.DataContextTransaction; + cmd.Transaction = transaction; cmd.CommandText = command; cmd.CommandType = CommandType.StoredProcedure; try { |