其中一个主键值的类型与实体中定义的类型不匹配。请参阅内部异常了解详细信

时间:2012-12-23 20:02:12

标签: c# entity-framework entity-framework-4

我有以下方法: -

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 个答案:

答案 0 :(得分:9)

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

这意味着您的ID课程的AccountDefinitionlongInt64,但您尝试使用string进行查询。

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

  1. string[]中的CustomersDetails(string[] SelectRight)更改为long[],将string中的GetAccount(string id)更改为long id
  2. return entities.AccountDefinition.Find(id);更改为return entities.AccountDefinition.Find(long.Parse(id));
  3. 选项1是更好的选择,但需要更多更改(我建议您这样做),选项2更改较少,但如果id为空或者值不可能,它可能会爆炸解析为long

答案 1 :(得分:1)

您传递给Find方法字符串,但期望Int64 The argument types 'Edm.Int64' and 'Edm.String'

答案 2 :(得分:1)

我知道这是一篇旧帖子,但我想我会在这里添加评论,因为我遇到了同样的问题。

我所做的就是在find函数中重新排列参数。

我喜欢这样:

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);
        }

我不得不改变它:

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);
        }

答案 3 :(得分:0)

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