我有使用混合EF和普通SQL调用的代码。整个过程在Azure上运行,因此我们使用ReliableSqlConnection。 我们正在使用TransactionScope,我们没有分布式事务管理器(Azure再次)。因此,我必须将ReliableSqlConnection传递给每个SQL调用。
现在的问题是如何将ReliableSqlConnection传递给EF调用?
如果发现这篇文章:
How to use ADO.net Entity Framework with an existing SqlConnection?
这将导致此代码:
MetadataWorkspace workspace = new MetadataWorkspace(
new string[] { "res://*/" },
new Assembly[] { Assembly.GetExecutingAssembly() });
using (var scope = new TransactionScope())
using (var conn = DatabaseUtil.GetConnection())
using (EntityConnection entityConnection = new EntityConnection(workspace, (DbConnection)conn))
using (var db = new UniversalModelEntities(entityConnection))
{
//Do EF things
//Call other SQL commands
return db.SaveChanges();
}
但我也不能将ReliableSqlConnection转换为DbConnection,UniversalModelEntities也不接受EntityConnection。
答案 0 :(得分:4)
问题是ReliableSqlConnection
实现IDbConnection
接口,但EF上下文构造函数都接受DbConnection
(不是接口)。我不知道他们为什么做出这样的决定,也许他们背后有一个有效的推理,也许这只是糟糕的设计决定。但是,你必须忍受这一点。请注意,使用ReliableSqlConnection.Open()
或ReliableSqlConnection.Current
返回的任何内容都不是一个选项 - 它可以正常工作,但您只需使用常规连接,而无需重试逻辑,基本上绕过整个ReliableSqlConnection类的目的。相反,您可能会尝试围绕ReliableSqlConnection创建一个包装器,如下所示:
public class ReliableSqlConnectionWrapper : DbConnection {
private readonly ReliableSqlConnection _connection;
public ReliableSqlConnectionWrapper(ReliableSqlConnection connection) {
_connection = connection;
}
protected override DbTransaction BeginDbTransaction(System.Data.IsolationLevel isolationLevel) {
return (DbTransaction) _connection.BeginTransaction();
}
public override void Close() {
_connection.Close();
}
public override void ChangeDatabase(string databaseName) {
_connection.ChangeDatabase(databaseName);
}
public override void Open() {
_connection.Open();
}
public override string ConnectionString
{
get { return _connection.ConnectionString; }
set { _connection.ConnectionString = value; }
}
public override string Database
{
get { return _connection.Database; }
}
public override ConnectionState State
{
get { return _connection.State; }
}
public override string DataSource
{
get { return _connection.Current?.DataSource; }
}
public override string ServerVersion
{
get { return _connection.Current?.ServerVersion; }
}
protected override DbCommand CreateDbCommand() {
return _connection.CreateCommand();
}
protected override DbProviderFactory DbProviderFactory {
get { return SqlClientFactory.Instance; }
}
}
这里我们继承了EF想要的DbConnection
,并将所有逻辑转发给底层的ReliableSqlConnection
实例。请注意,您可能需要覆盖DbConnection
中的更多方法(如Dispose
) - 这里我只展示如何仅覆盖必需(抽象)成员。
包装器的替代选项是复制ReliableSqlConnection
类的源代码并将其修改为继承DbConnection
。
然后,在您的EF上下文中,您需要添加一个接受DbConnection的构造函数:
public UniversalModelEntities(DbConnection connection, bool contextOwnsConnection) : base(connection, contextOwnsConnection) {}
它只使用相同的参数调用基类构造函数。第二个参数(contextOwnsConnection
)定义上下文是否能够管理此连接,例如在处理上下文时关闭它。
如果您使用EF数据库第一种方法 - 编辑EF模板,为您的上下文生成代码并在那里添加此构造函数。
毕竟,你可以这样做:
using (var scope = new TransactionScope()) {
using (var conn = new ReliableSqlConnection("")) {
using (var ctx = new UniversalModelEntities(new ReliableSqlConnectionWrapper(conn), false)) {
}
}
}
经过一番调查后,我得出结论,上面的方法可能难以实现,因为连接包装器与实体框架不完全兼容。考虑更简单的替代方法 - 使用DbCommandInterceptor并使用由提供ReliableSqlConnection的相同Transient Fault Handler库提供的扩展方法重用重试逻辑:
public class RetryInterceptor : DbCommandInterceptor {
public override void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext) {
interceptionContext.Result = ((SqlCommand)command).ExecuteNonQueryWithRetry();
}
public override void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext) {
interceptionContext.Result = ((SqlCommand)command).ExecuteReaderWithRetry();
}
public override void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext) {
interceptionContext.Result = ((SqlCommand)command).ExecuteScalarWithRetry();
}
}
因此我们拦截命令并将其执行转发到Transient Fault Handler块。然后在main方法中:
static void Main() {
// don't forget to add interceptor
DbInterception.Add(new RetryInterceptor());
MetadataWorkspace workspace = new MetadataWorkspace(
new string[] {"res://*/"},
new[] {Assembly.GetExecutingAssembly()});
// for example
var strategy = new FixedInterval("fixed", 10, TimeSpan.FromSeconds(3));
var manager = new RetryManager(new[] {strategy}, "fixed");
RetryManager.SetDefault(manager);
using (var scope = new TransactionScope()) {
using (var conn = new ReliableSqlConnection("data source=(LocalDb)\\v11.0;initial catalog=TestDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework")) {
// pass Current - we don't need retry logic from ReliableSqlConnection any more
using (var ctx = new TestDBEntities(new EntityConnection(workspace, conn.Current), false)) {
// some sample code I used for testing
var code = new Code();
code.Name = "some code";
ctx.Codes.Add(code);
ctx.SaveChanges();
scope.Complete();
}
}
}
}
答案 1 :(得分:1)
你试过了吗?
using (EntityConnection entityConnection =
new EntityConnection(workspace, (DbConnection)conn.Current))