From 03a923d247ddaf75febfb1b7b728e0c25fb21f33 Mon Sep 17 00:00:00 2001 From: BlairAlegroTech Date: Sun, 6 Jul 2014 20:11:36 +1200 Subject: [PATCH 1/2] Bug Fix: Prevent failure when executing SQL string (fixes #7) --- .../DbProviderTestBase.cs | 16 ++++++++ ReliableDbProvider/ReliableSqlCommand.cs | 38 +++++++++++++++---- ReliableDbProvider/ReliableSqlDbConnection.cs | 2 +- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/ReliableDbProvider.Tests/DbProviderTestBase.cs b/ReliableDbProvider.Tests/DbProviderTestBase.cs index bc46b4c..88329fa 100644 --- a/ReliableDbProvider.Tests/DbProviderTestBase.cs +++ b/ReliableDbProvider.Tests/DbProviderTestBase.cs @@ -38,6 +38,22 @@ public void Insert_and_select_entity() Assert.That(dbUser.Name, Is.EqualTo(user.Name)); } } + + [Test] + public void Insert_and_select_entity_with_custom_sql() + { + using (var context = GetContext()) + using (var context2 = GetContext()) + { + var user = new User { Name = "Name" }; + context.Users.Add(user); + context.SaveChanges(); + + var dbUser = context2.Database.SqlQuery("select * from [user] where Id = '" + user.Id + "'").FirstOrDefault(); + + Assert.That(dbUser.Name, Is.EqualTo(user.Name)); + } + } [Test] public void Insert_and_select_multiple_entities() diff --git a/ReliableDbProvider/ReliableSqlCommand.cs b/ReliableDbProvider/ReliableSqlCommand.cs index 7065ce3..78a3299 100644 --- a/ReliableDbProvider/ReliableSqlCommand.cs +++ b/ReliableDbProvider/ReliableSqlCommand.cs @@ -15,22 +15,39 @@ namespace ReliableDbProvider /// public class ReliableSqlCommand : DbCommand, ICloneable { + ReliableSqlDbConnection ReliableDbConnection; + /// /// The underlying being proxied. /// - public SqlCommand Current { get; private set; } + SqlCommand Current { get; set; } /// /// The that has been assigned to the command via the Connection property. /// - public ReliableSqlConnection ReliableConnection { get; set; } + ReliableSqlConnection ReliableConnection { get; set; } + /// - /// Constructs a . + /// Constructs a . with no associated connection /// - public ReliableSqlCommand(SqlCommand commandToWrap) + internal ReliableSqlCommand(SqlCommand commandToWrap) + { + this.Current = commandToWrap; + System.Diagnostics.Debug.Assert( + Current.Connection == null, + "Expected Command connection to be uninitialised. This constructor creates a new command witn no associated connection!"); + + this.ReliableDbConnection = null; + this.ReliableConnection = null; + } + + //Bug Fix: Failure when executing SQL string + public ReliableSqlCommand(ReliableSqlDbConnection connection, SqlCommand commandToWrap) { - Current = commandToWrap; + this.Current = commandToWrap; + this.ReliableDbConnection = connection; + this.ReliableConnection = (connection==null) ? null : connection.ReliableConnection; } /// @@ -48,19 +65,24 @@ public static explicit operator SqlCommand(ReliableSqlCommand command) /// protected override DbConnection DbConnection { - get { return Current.Connection; } + get + { + return ReliableDbConnection; + } set { if (value == null) return; - ReliableConnection = ((ReliableSqlDbConnection)value).ReliableConnection; + + ReliableDbConnection = ((ReliableSqlDbConnection)value); + ReliableConnection = ReliableDbConnection.ReliableConnection; Current.Connection = ReliableConnection.Current; } } public object Clone() { - return new ReliableSqlCommand(Current.Clone()); + return new ReliableSqlCommand(this.ReliableDbConnection, Current.Clone()); } #region Wrapping code diff --git a/ReliableDbProvider/ReliableSqlDbConnection.cs b/ReliableDbProvider/ReliableSqlDbConnection.cs index 3835368..a2ce672 100644 --- a/ReliableDbProvider/ReliableSqlDbConnection.cs +++ b/ReliableDbProvider/ReliableSqlDbConnection.cs @@ -95,7 +95,7 @@ public override void ChangeDatabase(string databaseName) protected override DbCommand CreateDbCommand() { - return new ReliableSqlCommand(ReliableConnection.CreateCommand()); + return new ReliableSqlCommand(this, ReliableConnection.CreateCommand()); } public override void Open() From 53d1392fd4e896d9fae9f3fd2bf332d816d3dd27 Mon Sep 17 00:00:00 2001 From: Rob Moore Date: Sun, 20 Jul 2014 12:01:46 +0800 Subject: [PATCH 2/2] Tidy up the code and make it consistent with the rest of the codebase --- ReliableDbProvider/ReliableSqlCommand.cs | 44 ++++++++++-------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/ReliableDbProvider/ReliableSqlCommand.cs b/ReliableDbProvider/ReliableSqlCommand.cs index 78a3299..f8378a1 100644 --- a/ReliableDbProvider/ReliableSqlCommand.cs +++ b/ReliableDbProvider/ReliableSqlCommand.cs @@ -2,6 +2,7 @@ using System.Data; using System.Data.Common; using System.Data.SqlClient; +using System.Diagnostics; using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.SqlAzure; namespace ReliableDbProvider @@ -15,39 +16,31 @@ namespace ReliableDbProvider /// public class ReliableSqlCommand : DbCommand, ICloneable { - ReliableSqlDbConnection ReliableDbConnection; - /// /// The underlying being proxied. /// - SqlCommand Current { get; set; } + private SqlCommand Current { get; set; } /// - /// The that has been assigned to the command via the Connection property. + /// The wrapper that has been assigned to the command via the Connection property or the ctor. /// - ReliableSqlConnection ReliableConnection { get; set; } - + private ReliableSqlDbConnection ReliableConnection { get; set; } /// /// Constructs a . with no associated connection /// internal ReliableSqlCommand(SqlCommand commandToWrap) { - this.Current = commandToWrap; - System.Diagnostics.Debug.Assert( - Current.Connection == null, - "Expected Command connection to be uninitialised. This constructor creates a new command witn no associated connection!"); - - this.ReliableDbConnection = null; - this.ReliableConnection = null; + Debug.Assert(commandToWrap.Connection == null, "Expected Command connection to be uninitialised. This constructor creates a new command with no associated connection."); + Current = commandToWrap; } - //Bug Fix: Failure when executing SQL string public ReliableSqlCommand(ReliableSqlDbConnection connection, SqlCommand commandToWrap) { - this.Current = commandToWrap; - this.ReliableDbConnection = connection; - this.ReliableConnection = (connection==null) ? null : connection.ReliableConnection; + Current = commandToWrap; + ReliableConnection = connection; + if (connection != null) + Current.Connection = ReliableConnection.ReliableConnection.Current; } /// @@ -67,22 +60,21 @@ protected override DbConnection DbConnection { get { - return ReliableDbConnection; + return ReliableConnection; } set { if (value == null) return; - ReliableDbConnection = ((ReliableSqlDbConnection)value); - ReliableConnection = ReliableDbConnection.ReliableConnection; - Current.Connection = ReliableConnection.Current; + ReliableConnection = ((ReliableSqlDbConnection) value); + Current.Connection = ReliableConnection.ReliableConnection.Current; } } public object Clone() { - return new ReliableSqlCommand(this.ReliableDbConnection, Current.Clone()); + return new ReliableSqlCommand(ReliableConnection, Current.Clone()); } #region Wrapping code @@ -115,9 +107,9 @@ protected override DbParameter CreateDbParameter() protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior) { - return ReliableConnection.CommandRetryPolicy.ExecuteAction(() => { + return ReliableConnection.ReliableConnection.CommandRetryPolicy.ExecuteAction(() => { if (Connection == null) - Connection = ReliableConnection.Open(); + Connection = ReliableConnection.ReliableConnection.Open(); if (Connection.State != ConnectionState.Open) Connection.Open(); return Current.ExecuteReader(behavior); @@ -126,12 +118,12 @@ protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior) public override int ExecuteNonQuery() { - return ReliableConnection.ExecuteCommand(Current); + return ReliableConnection.ReliableConnection.ExecuteCommand(Current); } public override object ExecuteScalar() { - return ReliableConnection.ExecuteCommand(Current); + return ReliableConnection.ReliableConnection.ExecuteCommand(Current); } protected override DbTransaction DbTransaction