如何在EF 4.0中实现“以用户身份执行”

时间:2013-10-22 10:12:14

标签: c# .net entity-framework

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

我尝试了以下内容:

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()

我见过this question,但我没有得到答案。

0 个答案:

没有答案