Csharp/C#教程:其中一个主键值的类型与实体中定义的类型不匹配。 请参阅内部exception了解详细信分享


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

我有以下方法: –

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 

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

这意味着您的AccountDefinition类的IDlongInt64但是您尝试使用string查询它。

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

  1. CustomersDetails(string[] SelectRight)long[] ,将GetAccount(string id) string GetAccount(string id)long id
  2. 更改return entities.AccountDefinition.Find(id); return entities.AccountDefinition.Find(long.Parse(id));

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

您传递给Find方法字符串但延伸到Int64 The argument types 'Edm.Int64' and 'Edm.String'

我知道这是一个老post,但我想我在这里添加评论,因为我有同样的问题。

我所做的就是在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); } 

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

上述就是C#学习教程:其中一个主键值的类型与实体中定义的类型不匹配。 请参阅内部exception了解详细信分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1029884.html

(0)
上一篇 2022年1月13日
下一篇 2022年1月13日

精彩推荐