private IDatabase CreateDatabase(ContextServiceContext context) { var factory = context.ServiceProvider.TryGetService <IDatabaseFactory>(); if (factory != null) { return(factory.Create(context.Options)); } IDatabase database; if (context.Options.Provider != null && context.Options.ConnectionString != null) { database = new Database(context.Options.ConnectionString, context.Options.Provider).TrySetServiceProvider(context.ServiceProvider); } else { throw new InvalidOperationException(SR.GetString(SRKind.NotSupportDatabaseFactory)); } if (EntityTransactionScope.IsInTransaction()) { database.BeginTransaction(); EntityTransactionScope.Current.Addransaction((string)context.Options.ConnectionString, this); } return(database); }
/// <summary> /// 回滚数据库事务。 /// </summary> public void RollbackTransaction() { if (EntityTransactionScope.IsInTransaction()) { return; } Database.RollbackTransaction(); }
/// <summary> /// 提交数据库事务。 /// </summary> public void CommitTransaction() { if (EntityTransactionScope.IsInTransaction()) { return; } Database.CommitTransaction(); }
/// <summary> /// 释放资源。 /// </summary> /// <param name="disposing"></param> protected override bool Dispose(bool disposing) { if (EntityTransactionScope.IsInTransaction()) { return(false); } RollbackTransaction(); if (Database != null) { Database.TryDispose(disposing); _database = null; } return(base.Dispose(disposing)); }