阻止EF Core使用合并插入 [英] Stop EF Core from using Merge to Insert

查看:341
本文介绍了阻止EF Core使用合并插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的应用程序转换为.NET Core。这样,我遇到了EF Core和插入的问题。

I am converting my application to .NET Core. In doing so, I am running into issues with EF Core and inserts.

如果我插入1或2行,则EF Core执行普通的SQL INSERT 语句。

If I insert 1 or 2 rows then EF Core performs a normal SQL INSERT statement.

但是当我有3行或更多行时,它会切换到 MERGE 语句,该语句将失败并显示:

But when I have 3 or more rows, it switches to a MERGE statement, which then fails with:


不允许使用列引用 inserted.MyKeyColumn,因为它引用的是未在此语句中修改的基表。

The column reference "inserted.MyKeyColumn" is not allowed because it refers to a base table that is not being modified in this statement.

我的猜测是,这是由于查询实际上在具有插入触发器的视图上运行以更新

My guess is that this is due to the fact that the query is actually running on a view that has an insert trigger on it to update the actual table(s) under the view.

就像我说的那样,当它使用插入语句时,这种方法就很好了。

Like I said, this works just fine when it uses insert statements. But it fails when it tries to insert using a merge.

有没有办法阻止EF Core使用 MERGE 用于插入?

Is there a way to stop EF Core from using MERGE for inserts?

推荐答案

因此,更多研究显示了一种强制使用单个插入的方法:

So some more research showed a way to force the use of single inserts:

 optionsBuilder.UseSqlServer(connectionString, options =>
        {
            options.MaxBatchSize(1);
        });

警告:这将导致所有插入都是单独的插入语句。对于大容量刀片来说效果不佳。

Warning: This will cause all inserts to be individual insert statements. Not very performant for high volume inserts.

这篇关于阻止EF Core使用合并插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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