2

EXECUTE AS USER = 'xxx'出于安全原因,有没有办法在 Entity Framework 4.0 中实施该原则?

我尝试了以下方法:

public  class ImpersonateDatabaseEntities : DatabaseEntities
{
    private String _impersonateUser = null;

    public ImpersonateDatabaseEntities():base()
    {                   
       this._impersonateUser = "ProgUser";            
       base.Connection.StateChange += new System.Data.StateChangeEventHandler(Connection_StateChange);            
    }

    void Connection_StateChange(object sender, System.Data.StateChangeEventArgs e)
    {
        if (e.CurrentState == System.Data.ConnectionState.Open &&
             e.OriginalState != System.Data.ConnectionState.Open)
        {
            this.ExecuteStoreCommand("REVERT; EXECUTE AS USER = 'ProgUser';");
        }
    }
}

我总是在 SaveChanges() 上遇到错误,但我不知道如何处理这个问题。
这是我收到的错误消息:

Message: Failed to start a transaction on the provider connection. For more information, see the inner exception. 
Stacktrace: System.Data.SqlClient.SqlException (0x80131904): A severe error occurred on the current command. Cancel any results
  at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
  at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
  at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
  at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
  at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
  at System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)
  at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest, String transactionName, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
  at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransaction(TransactionRequest transactionRequest, String name, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
  at System.Data.SqlClient.SqlInternalConnection.BeginSqlTransaction(IsolationLevel iso, String transactionName)
  at System.Data.SqlClient.SqlInternalConnection.BeginTransaction(IsolationLevel iso)
  at System.Data.SqlClient.SqlConnection.BeginDbTransaction(IsolationLevel isolationLevel)
  at System.Data.Common.DbConnection.BeginTransaction(IsolationLevel isolationLevel)
  at System.Data.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel)
  ClientConnectionId:7aae526f-706f-4745-a3fd-2274f50be6ce

  at System.Data.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel)
  at System.Data.EntityClient.EntityConnection.BeginTransaction()
  at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
  at System.Data.Objects.ObjectContext.SaveChanges()

我已经看到了这个问题,但我没有从中得到答案。

4

0 回答 0