使用 Entity Framework 5 与 Oracle 11g 对话,为每个会话执行 SQL Alter 命令 [英] Execute SQL Alter commands for every session with Entity Framework 5 talking to Oracle 11g

查看:19
本文介绍了使用 Entity Framework 5 与 Oracle 11g 对话,为每个会话执行 SQL Alter 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在每个数据库会话开始时执行一些 SQL 命令.我正在通过 DbContext 使用 Entity Framework 5 与 Oracle 11g 数据库交谈.

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.

我想执行:

ALTER SESSION SET NLS_COMP=ANSI;
ALTER SESSION SET NLS_SORT=BINARY_CI;

在会话创建开始时进行不区分大小写的搜索.
我该怎么做才能做到最好?

at the start of an session creation to get case insensitive searching.
How best could I go about this?

我已将命令放在 dbContext 的构造函数中,但只有简单的单元测试,而且它似乎确实有效.但不确定这样做是否正确

I've put the commands in the constructor of dbContext, but have only simple unit test and it does appear to work. But unsure if this is right thing to do

public partial class Entities : DbContext
{
    public Entities()
        : base("name=Entities")
    {
        this.Database.ExecuteSqlCommand("ALTER SESSION SET NLS_COMP=ANSI");
        this.Database.ExecuteSqlCommand("ALTER SESSION SET NLS_SORT=BINARY_CI");
    }
}

推荐答案

如果有人正在阅读本文,不,正确的方法是在以下行触发的连接打开时执行此操作:

If anyone is reading this, no, the right way is doing it on connection open triggered down the line of:

public Entities()
    : base("name=Entities")
{        

    ctx.Database.Connection.StateChange += Connection_StateChange;
    ...
}

    private void Connection_StateChange(object sender, StateChangeEventArgs e)
    {
        if (e.OriginalState == ConnectionState.Open || e.CurrentState != ConnectionState.Open)
            return;

       this.Database.ExecuteSqlCommand("ALTER SESSION SET NLS_COMP=ANSI");

    }

这篇关于使用 Entity Framework 5 与 Oracle 11g 对话,为每个会话执行 SQL Alter 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆