gpt4 book ai didi

c# - 其中一个主键值的类型与实体中定义的类型不匹配。有关详细信息,请参见内部异常

转载 作者:太空狗 更新时间:2023-10-29 21:09:38 34 4
gpt4 key购买 nike

我有以下方法:-

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

最佳答案

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

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

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

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

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

关于c# - 其中一个主键值的类型与实体中定义的类型不匹配。有关详细信息,请参见内部异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14014271/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com