7

我有以下方法: -

public ActionResult CustomersDetails(string[] SelectRight)
{
    var selectedCustomers = new SelectedCustomers
    {
        Info = SelectRight.Select(GetAccount)
    };

    return View(selectedCustomers);
}

private AccountDefinition GetAccount(string id)
{
    return entities.AccountDefinition.Find(id);
}

但它返回以下错误:-

The type of one of the primary key values did not match the type defined in the entity. See inner exception for details.

return entities.AccountDefinition.Find(id);线

那么是什么导致了这个错误呢?

内部例外是:-

System.ArgumentException was unhandled by user code
  HResult=-2147024809
  Message=The type of one of the primary key values did not match the type defined in the entity. See inner exception for details.
Parameter name: keyValues
  Source=EntityFramework
  ParamName=keyValues
  StackTrace:
       at System.Data.Entity.Internal.Linq.InternalSet`1.FindInStore(WrappedEntityKey key, String keyValuesParamName)
       at System.Data.Entity.Internal.Linq.InternalSet`1.Find(Object[] keyValues)
       at System.Data.Entity.DbSet`1.Find(Object[] keyValues)

  InnerException: System.Data.EntitySqlException
       HResult=-2146232006
       Message=The argument types 'Edm.Int64' and 'Edm.String' are incompatible for this operation. Near WHERE predicate, line 1, column 90.
       Source=System.Data.Entity
       Column=90
       ErrorContext=WHERE predicate, line 1, column 90
       ErrorDescription=The argument types 'Edm.Int64' and 'Edm.String' are incompatible for this operation.
       Line=1
4

4 回答 4

11

查看异常消息The argument types 'Edm.Int64' and 'Edm.String' are incompatible for this operation. Near WHERE predicate, line 1, column 90.

这意味着ID您的AccountDefinition班级的 是 along或者Int64您尝试使用 a 来查询它string

您需要执行以下操作之一:

  1. 更改string[]为和更改CustomersDetails(string[] SelectRight)为_long[]stringGetAccount(string id)long id
  2. 更改return entities.AccountDefinition.Find(id);return entities.AccountDefinition.Find(long.Parse(id));

选项 1 是更好的选项,但需要更多更改(我建议您这样做),选项 2 更改较少,但如果id为 null 或无法解析为long.

于 2012-12-23T20:20:35.360 回答
2

I know this is an old post but I figured I'd add a comment here since I had the same problem.

All I did was re-arrange the parameters in the find function.

I had it like this:

public ActionResult Details(Int32 id, string dataSource)
        {
            TVData_VW_ShowList tvdata_vw_showlist = context.TVData_VW_ShowList.Find(id, datasource);
            if (tvdata_vw_showlist == null)
            {
                return HttpNotFound();
            }
            return View(tvdata_vw_showlist);
        }

And I had to change it to this:

public ActionResult Details(Int32 id, string dataSource)
        {
            TVData_VW_ShowList tvdata_vw_showlist = context.TVData_VW_ShowList.Find(dataSource, id);
            if (tvdata_vw_showlist == null)
            {
                return HttpNotFound();
            }
            return View(tvdata_vw_showlist);
        }
于 2013-06-07T17:35:46.500 回答
1

您传递给 Find 方法 string 但预期为 Int64 The argument types 'Edm.Int64' and 'Edm.String'

于 2012-12-23T20:11:59.533 回答
1

我花了很多时间来解决这个问题,但最后发现 .Find(Key1, Key2, ..) 中的键序列应该与 Edmx 图实体中的键序列匹配。

于 2017-04-26T08:33:46.740 回答