参数集合中已经存在一个名为“p__linq__0”的参数。参数名称必须在参数集合中是唯一的。

时间:2022-04-16 20:54:17

This is a strange error I'm getting from Entity Framework: "A parameter named 'p_linq_0' already exists in the parameter collection. Parameter names must be unique in the parameter collection." I am not doing any custom parameters or anything fancy, simply straight LINQ queries or stored procedure executions, nothing fancy, nothing out of the ordinary, no Entity SQL... so why would this error happen?

这是我从Entity Framework获得的一个奇怪的错误:“一个名为'p_linq_0'的参数已经存在于参数集合中。参数名称必须在参数集合中是唯一的。我没有做任何自定义参数或任何花哨的东西,简单的LINQ查询或存储过程的执行,没有任何幻想,没有任何东西,没有实体SQL……那么,为什么会出现这种错误呢?

Thanks.

谢谢。

2 个解决方案

#1


2  

I had the same issue - I was running several queries in parallel in their own Task.Runs.

我有同样的问题——我在自己的任务中并行运行几个查询。

Each of my queries used a list of Ids that I was populating with var ids = _someList.Select(x => x.Thing.Id).Distinct()

我的每一个查询都使用了一个id列表,我使用的是var id = _someList。选择(x = > x.Thing.Id).Distinct()

Constructing the parameters of the multiple queries, I was doing string.Join(",", ids) in several of them.

构造多个查询的参数时,我在做字符串。在其中的几个中加入(“,”,ids)。

The answer eventually became apparent: deferred execution.

答案最终变得明显:推迟执行。

Each query, at the point where it was doing string.Join(",", ids), finally executed the query for the ids (at the same time) causing the duplicate parameter (p_linq_0).

每个查询,在它执行字符串的时候。Join(“,”,ids)最终执行了对ids的查询(同时)导致了重复参数(p_linq_0)。

Simply adding .ToList() to execute on that line meant the queries had a concrete list of Ids to use and weren't all trying to do it at the same time themselves.

简单地添加. tolist()在这一行上执行,意味着查询有一个具体的id列表,并不是所有人都试图在同一时间完成它。

var ids = _someList.Select(x => x.Thing.Id).Distinct().ToList();

var id = _someList。选择(x = > x.Thing.Id).Distinct().ToList();

#2


0  

Without seeing your code, we can't possibly figure out what your problem is. It sounds like there are a few things that could cause this. I seem to have discovered one, though. I have a model whose primary key is an enum, so I was doing this:

没有看到你的代码,我们不可能知道你的问题是什么。听起来好像有一些事情会导致这种情况。不过,我似乎已经发现了一个。我有一个模型,它的主键是enum,所以我这样做:

context.Tests.Where(t => t.TypeId == TestTypeId.SingleTimeWholeClass)

Inexplicably, casting each side of the comparison to an int fixed my problem:

令人费解的是,把每一个方面都与一个整数进行比较,我的问题是:

context.Tests.Where(t => (int)t.TypeId == (int)TestTypeId.SingleTimeWholeClass)

Could be totally unrelated to your problem. Posting code always helps.

可能与你的问题完全无关。发布代码总是帮助。

#1


2  

I had the same issue - I was running several queries in parallel in their own Task.Runs.

我有同样的问题——我在自己的任务中并行运行几个查询。

Each of my queries used a list of Ids that I was populating with var ids = _someList.Select(x => x.Thing.Id).Distinct()

我的每一个查询都使用了一个id列表,我使用的是var id = _someList。选择(x = > x.Thing.Id).Distinct()

Constructing the parameters of the multiple queries, I was doing string.Join(",", ids) in several of them.

构造多个查询的参数时,我在做字符串。在其中的几个中加入(“,”,ids)。

The answer eventually became apparent: deferred execution.

答案最终变得明显:推迟执行。

Each query, at the point where it was doing string.Join(",", ids), finally executed the query for the ids (at the same time) causing the duplicate parameter (p_linq_0).

每个查询,在它执行字符串的时候。Join(“,”,ids)最终执行了对ids的查询(同时)导致了重复参数(p_linq_0)。

Simply adding .ToList() to execute on that line meant the queries had a concrete list of Ids to use and weren't all trying to do it at the same time themselves.

简单地添加. tolist()在这一行上执行,意味着查询有一个具体的id列表,并不是所有人都试图在同一时间完成它。

var ids = _someList.Select(x => x.Thing.Id).Distinct().ToList();

var id = _someList。选择(x = > x.Thing.Id).Distinct().ToList();

#2


0  

Without seeing your code, we can't possibly figure out what your problem is. It sounds like there are a few things that could cause this. I seem to have discovered one, though. I have a model whose primary key is an enum, so I was doing this:

没有看到你的代码,我们不可能知道你的问题是什么。听起来好像有一些事情会导致这种情况。不过,我似乎已经发现了一个。我有一个模型,它的主键是enum,所以我这样做:

context.Tests.Where(t => t.TypeId == TestTypeId.SingleTimeWholeClass)

Inexplicably, casting each side of the comparison to an int fixed my problem:

令人费解的是,把每一个方面都与一个整数进行比较,我的问题是:

context.Tests.Where(t => (int)t.TypeId == (int)TestTypeId.SingleTimeWholeClass)

Could be totally unrelated to your problem. Posting code always helps.

可能与你的问题完全无关。发布代码总是帮助。