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

Joh*_*ter 6 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);
}
Run Code Online (Sandbox Code Playgroud)

但它返回以下错误: -

The type of one of the primary key values did not match the type defined in the entity. See inner exception for details.
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Tre*_*ley 9

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

这意味着ID您的AccountDefinition类是long或者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或无法解析为a的值,则可能会爆炸long.