1.定义EF拦截器,截获执行命令前的操作。修改执行sql。还需要定义orcal序列,供自增使用

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity.Infrastructure.Interception;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
/*
* CREATE SEQUENCE "SQ_IHR_ID" INCREMENT BY 1 START WITH 1 NOMAXVALUE NOCYCLE NOCACHE;
*/ namespace TechFancier.Data
{ public class NoLockInterceptor : DbCommandInterceptor
{
static bool isDebug = false;
static NoLockInterceptor()
{
isDebug = System.Configuration.ConfigurationManager.AppSettings["DBDebug"] == "true";
} private static readonly Regex _tableAliasRegex =
new Regex(@"(?<tableAlias>AS \[Extent\d+\](?! WITH \(NOLOCK\)))",
RegexOptions.Multiline | RegexOptions.IgnoreCase); [ThreadStatic]
public static bool SuppressNoLock; public override void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
base.NonQueryExecuting(ReformerSql(command), interceptionContext);
} public override void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
base.ScalarExecuting(ReformerSql(command), interceptionContext);
} public override void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
base.ReaderExecuting(ReformerSql(command), interceptionContext);
}
//测试用
static System.IO.StreamWriter sw = new System.IO.StreamWriter("c.txt", true, Encoding.Default);
static void writeLog(string msg)
{
if (isDebug)
{
sw.WriteLine("[{0}][{1}]:{2}", System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msg);
sw.Flush();
}
}
protected DbCommand ReformerSql(DbCommand command)
{
writeLog("原始语句:" + command.CommandText);
StringBuilder sb = new StringBuilder();
foreach (DbParameter item in command.Parameters)
{
if (item != null)
sb.AppendFormat("key:{0} value:{1} valueType:{2} ", item.ParameterName, item.Value, item.Value == null ? "" : item.Value.GetType().Name);
} writeLog("参数:" + sb);
if (command.CommandText.Contains("create table"))
{//处理C#与orcal字符串不兼容的问题。
command.CommandText = command.CommandText.Replace("\" nclob", "\" varchar2(1000)");
}
//判断执行语句是否为插入语句(是)
if (command.CommandText.IndexOf("insert into", StringComparison.InvariantCultureIgnoreCase) >= )
{
string match = @"insert into ""\w+"".""\w+""\(";
string match2 = @""".""\w+""\(""";
//判断插入语句中是否包括插入Id字段(否)
if (command.CommandText.IndexOf("\"Id\",") < )
{
string val = Regex.Match(command.CommandText, match).Value;
string obj = Regex.Match(command.CommandText, match2).Value;
string table = obj.Substring(, obj.Length - );
//如果表名是Hrmanagers,FunMenu,则不需要做任何处理,因为它在插入的时候已经带上了User_Id
if (table != "HrManagers" && !table.Contains("FunMenu"))
{
command.CommandText = command.CommandText.Replace(val, val + "\"Id\", ");
command.CommandText = command.CommandText.Replace("values (", "values (SQ_IHR_ID.NEXTVAL, ");
writeLog("修改后的语句:" + command.CommandText);
}
return command; }
}
return command;
}
}
}
//
//command.CommandText = command.CommandText.Replace("values (", "values (\"SQ_" + table + "_Id\".NEXTVAL, ");

2.在合适的位置注入拦截器

                System.Data.Entity.Infrastructure.Interception.DbInterception.Add(new NoLockInterceptor());

Orcal数据库,使用EF的自增处理的更多相关文章

  1. Entity - 使用EF框架进行增删改查 - 模型先行

    模型先行:先创建数据库实体模型,然后再进行数据库的增删改查. 基本步骤是不变的,可参照 <Entity - 使用EF框架进行增删改查 - 数据库先行> 其中的不同是,在创建数据库实体模型的 ...

  2. (18)ASP.NET Core 基于现有数据库创建EF模型(反向工程)

    1.简介 Entity Framework Core可通过数据库提供给应用程序的插件访问许多不同的数据库.我们可以通过使用Entity Framework Core构建执行基本数据访问的ASP.NET ...

  3. Codesmith怎么判断sqlserver数据库字段是不是标识自增字段

    Codesmith怎么判断sqlserver数据库字段是不是标识自增字段 使用ExtendedProperty扩展信息判断 CS_isIdentity:是否为标识符,不支持Access CS_isCo ...

  4. VS2012、2013使用Mysql数据库创建EF的AOD.NET实体模型

    VS2012.2013使用Mysql数据库创建EF的AOD.NET实体模型: 1.关闭VS,首先安装mysql-connector-net-6.8.3.(安装后EF创建实体模型时就可以找到Mysql的 ...

  5. 一、数据库表中字段的增删改查,二、路由基础.三、有名无名分组.四、多app共存的路由分配.五、多app共存时模板冲突问题.六、创建app流程.七、路由分发.八、路由别名,九、名称空间.十、反向解析.十一、2.x新特性.十二、自定义转换器

    一.数据库表中字段的增删改查 ''' 直接在modules中对字段进行增删改查 然后在tools下点击Run manage.py Task执行makemigrations和migrate 注意在执行字 ...

  6. Orcal数据库12c安装完成后注意事项

    1.按照12c的安装使用装完数据库后,使用navicat链接orcal数据库时无法使用. 点击此处查看安装方案 2. 2048错误,ORA-28040: No matching authenticat ...

  7. 用CI框架向数据库中实现简单的增删改查

    以下代码基于CodeIgniter_2.1.3版 用PHP向数据库中实现简单的增删改查(纯代码)请戳 http://www.cnblogs.com/corvoh/p/4641476.html Code ...

  8. orcal数据库得连接必须用localhost,url中不要用127.0.0.1,不然无法连接

    orcal数据库得连接必须用localhost,url中不要用127.0.0.1,不然无法连接,

  9. Django框架之第二篇--app注册、静态文件配置、form表单提交、pycharm连接数据库、django使用mysql数据库、表字段的增删改查、表数据的增删改查

    本节知识点大致为:静态文件配置.form表单提交数据后端如何获取.request方法.pycharm连接数据库,django使用mysql数据库.表字段的增删改查.表数据的增删改查 一.创建app,创 ...

  10. [Django框架 - 静态文件配置、request对象方法初识、 pycharm链接数据库、ORM实操增删改查、django请求生命周期]

    [Django框架 - 静态文件配置.request对象方法初识. pycharm链接数据库.ORM实操增删改查.django请求生命周期] 我们将html文件默认都放在templates文件夹下 将 ...

随机推荐

  1. Picasso加载网络图片失败,提示decodestream时返回null

    最近遇到一个问题,项目用的图片加载框架是Picasso,网络加载框架是okhttp,项目在加载轮播图时有时可以正常加载,有时,会加载失败,提示decodestream时返回null. 首先,需要确定是 ...

  2. Java虚拟机(五):JVM调优命令

    运用jvm自带的命令可以方便的在生产监控和打印堆栈的日志信息帮忙我们来定位问题!虽然jvm调优成熟的工具已经有很多:jconsole.大名鼎鼎的VisualVM,IBM的Memory Analyzer ...

  3. test11

    -Xms512m-Xmx512m-XX:PermSize=512-XX:MaxPermSize=512

  4. mysql RR下不存在则插入

    主要看并发事务中不存在则插入(只有key索引)的阻塞情况. 表定义: mysql> desc user; +-------------+------------------+------+--- ...

  5. PTA (Advanced Level) 1024 Palindromic Number

    Palindromic Number A number that will be the same when it is written forwards or backwards is known ...

  6. linux 查找删除找定文件

    find . -name "*.lastUpdated" -exec rm -rf {} \; 这个命令是find的基本用法,可以分两部分,find ~/ -name " ...

  7. import.html 页面导出execl

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. SQLite菜鸟教程

    学习链接:http://www.runoob.com/sqlite/sqlite-trigger.html

  9. Java Swing实战(二)下拉菜单组件JComboBox及其事件监听

    接下来给"数据源配置"面板添加下拉框. /** * @author: lishuai * @date: 2018/11/26 13:51 */ public class Weimi ...

  10. vue 实现父组件和子组件之间的数据双向绑定

    前言:vue 实现父组件给子组件传值,然后子组件可以修改回父组件的值.vue 的 prop 默认是单向数据绑定,但是偶尔需要双向绑定,这时就需要知道如何才能让子组件的数据修改时影响到父组件的数据.转载 ...