Stop EF Core from using Merge to Insert

五迷三道 提交于 2020-01-05 07:16:43

问题


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

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

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

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.

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


回答1:


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.



来源:https://stackoverflow.com/questions/59183119/stop-ef-core-from-using-merge-to-insert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!