c# - 如何在实体框架中拦截和更改插入语句
问题描述
最近我不得不向现有的数据仓库添加一个新的数据源。这个数据源有一些表字段超过了我在数据仓库中的当前字段大小。由于非技术原因,我不应该更改 Datawarehouse 的 Fieldsize。
我想要做的是使用实体框架的拦截系统来检查插入和修剪字段到定义的长度(在属性中可用,代码优先方法)
到目前为止我得到了什么:
public class StringTrimmerInterceptor : IDbCommandTreeInterceptor
{
void IDbCommandTreeInterceptor.TreeCreated(DbCommandTreeInterceptionContext interceptionContext)
{
if (interceptionContext.OriginalResult.DataSpace != DataSpace.SSpace)
return;
var insertCommand = interceptionContext.Result as DbInsertCommandTree;
if (insertCommand != null)
{
//Check and Trim oversized Fields
}
}
}
上面的代码可以找到我想要更改的正确插入语句。我想知道的是如何实际更改和执行它们。
我确实找到了一些关于如何更改查询的示例,但没有真正更改插入/更新。
结果应该是这样的:如果我尝试使用名称保存客户:“MyCustomerWithanUnreasonableLongName”,我只想保存模型中指定的字符数,例如“MyCustomerWitha”
解决方案
推荐阅读
- javascript - typescript 中 getter 和 setter 的“计算属性名称”
- mongodb - MongoDB获取数组的值
- javascript - 滚动时如何在画布上继续绘图?
- visual-studio-code - 在 Visual Studio Code 中使用“type”:“lime”的任务有什么作用?
- scala - SparkML (Scala) 中的并行训练独立模型
- python - 什么是线性回归模型中的 r 平方?
- html - 为什么标题文本没有与其余内容水平对齐?
- python - Pytest - 无法访问导入的类的属性?
- javascript - 如何使用 Google Scripts 和 Google Sheets 执行笛卡尔连接?
- windows - 来自一个 FOR 循环的变量作为批量嵌套 FOR 循环的路径