加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 其中一个主键值的类型与实体中定义的类型不匹配.请参阅内

发布时间:2020-12-15 08:33:21 所属栏目:百科 来源:网络整理
导读:我有以下方法: – public ActionResult CustomersDetails(string[] SelectRight){ var selectedCustomers = new SelectedCustomers { Info = SelectRight.Select(GetAccount) }; return View(selectedCustomers);}private AccountDefinition GetAccount(str
我有以下方法: –
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,column 90
       ErrorDescription=The argument types 'Edm.Int64' and 'Edm.String' are incompatible for this operation.
       Line=1

解决方法

查看异常消息参数类型’Edm.Int64’和’Edm.String’与此操作不兼容.在WHERE谓词附近,第1行,第90列..

这意味着您的AccountDefinition类的ID是long或Int64,但是您尝试使用字符串查询它.

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

>将CustomersDetails(string [] SelectRight)中的string []更改为long [],将GetAccount(字符串id)中的字符串更改为long id
>更改返回实体.AccountDefinition.Find(id);返回entities.AccountDefinition.Find(long.Parse(id));

选项1是更好的选择,但需要更多更改(我建议你这样做),选项2更改较少,但如果id为null或无法解析为long的值,则可能会爆炸.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读