简体   繁体   中英

Retry policy in Entity Framework

I want to implement retry policy in a project I am working on which uses Entity Framework connecting to MSSQL 2012. I can do the following:

using (var model = new MyModel())
{
    for (var i = 0; i < 3; i++)
    {
        try
        {
            model.MyEntities.Where(x => x.Index < 1000);
            break;
        }
        catch (Exception ex)
        {
        }
    }
}

but it is quite cumbersome to do this everywhere I want to make a query using the model. Is there anyway to implement this in the model itself so that I don't have to worry about it?

One way I thought of is to use inherit the DbCommand and override ExecuteScalar() , ExecuteNonQuery() , etc., so that they repeat the call to the methods of the base class for a certain number of times, but wondering whether there is an easier way to do that?

Currently it's not possible to do "using the model". You have to implement it manually. Either in ie repository or in some "data access adapter" if you're using something like that. Or if you want to go hard core :), you can, of course, create your own IDbConnection, IDbCommand etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM