如何在实体框架中拦截和更改插入语句

时间:2019-04-09 09:52:28

标签: c# entity-framework

最近,我不得不向现有的数据仓库中添加一个新的数据源。该数据源的某些表字段超出了我在数据仓库中当前的字段大小。由于非技术原因,我不应该更改数据仓库的字段大小。

我想做的是使用实体框架的拦截系统来检查“插入”和“修剪”字段是否达到定义的长度(可用于属性,代码优先方法)

我到目前为止所得到的:

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”

0 个答案:

没有答案