Execute SQL Alter commands for every session with Entity Framework 5 talking to Oracle 11g

前端 未结 3 1469
一生所求
一生所求 2021-01-13 18:00

I have a requirement to execute some SQL commands at the start of every database session. I am using Entity Framework 5 via DbContext talking to a Oracle 11g database.

3条回答
  •  庸人自扰
    2021-01-13 18:55

    you can use Database.Connection.StateChange method

        public AtomContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
            this.Database.Connection.StateChange += Connection_StateChange;
        }
    
        void Connection_StateChange(object sender, StateChangeEventArgs e)
        {
            if (e.OriginalState == ConnectionState.Open || e.CurrentState != ConnectionState.Open)
                return;
    
            IDbConnection connection = ((EntityConnection)((IObjectContextAdapter)this).ObjectContext.Connection).StoreConnection;
            using (IDbCommand command = connection.CreateCommand("ALTER SESSION SET NLS_LANGUAGE=TURKISH"))
                command.ExecuteNonQuery();
    
            using (IDbCommand command = connection.CreateCommand("ALTER SESSION SET NLS_COMP = LINGUISTIC"))
                command.ExecuteNonQuery();
    
            using (IDbCommand command = connection.CreateCommand("ALTER SESSION SET NLS_SORT=TURKISH_AI"))
                command.ExecuteNonQuery();
        }
    

    entityframeworkef6oracledbcontextsession

提交回复
热议问题