问题描述
我需要在每个数据库会话开始时执行一些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");
}
这篇关于使用与Oracle 11g通讯的Entity Framework 5为每个会话执行SQL Alter命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!