I would like to implement a broadcast application system for all calls to the Entity SaveChanges method.
Technology:
Entity framework 4.0
.Net 4.0
namespace Sample.Data.Store.Entities {
public partial class StoreDB
{
public override int SaveChanges(System.Data.Objects.SaveOptions options)
{
for (Int32 attempt = 1; ; )
{
try
{
return base.SaveChanges(options);
}
catch (SqlException sqlException)
{
attempt++;
Int32 maxRetryCount = 5;
if (attempt == maxRetryCount)
throw;
if (!RetryLitmus(sqlException))
throw;
else
Thread.Sleep(ConnectionRetryWaitSeconds(attempt));
}
}
}
static Int32 ConnectionRetryWaitSeconds(Int32 attempt)
{
Int32 connectionRetryWaitSeconds = 2000;
connectionRetryWaitSeconds = connectionRetryWaitSeconds *
(Int32)Math.Pow(2, attempt);
return (connectionRetryWaitSeconds);
}
static Boolean RetryLitmus(SqlException sqlException)
{
switch (sqlException.Number)
{
case 40197:
case 40501:
case 10053:
return (true);
}
return (false);
}
}
}
Problem:
How can I run StoreDB.SaveChanges to restart in the new DB context after an error occurs? You might need something simulative for disconnecting / attaching.
Thanks in advance! Bart
source
share