简体   繁体   English

无法将类型为“ System.Int64”的对象转换为类型为“ System.Int32”的对象

[英]Unable to cast object of type 'System.Int64' to type 'System.Int32'

Getting the error when asking for equal type of string in 在请求相等类型的字符串时出现错误

var orderTransferFromDb = await context.OrderTransfer.FirstOrDefaultAsync(t =>
t.ToMemberMobilePhone.ToUpperInvariant().Equals(mobilePhone.ToUpperInvariant()));

public async Task<bool> UpdateOrderTransferToMemberId(string mobilePhone, string memberid)
{
    if (mobilePhone != null)
    {
        using (var context = ContextManager.ClubContext())
        {
            var orderTransferFromDb = await context.OrderTransfer.FirstOrDefaultAsync(t => t.ToMemberMobilePhone.ToUpperInvariant().Equals(mobilePhone.ToUpperInvariant()));
            if (orderTransferFromDb != null)
            {
                context.Attach(orderTransferFromDb);
                orderTransferFromDb.ToMemberId = memberid;
                await context.SaveChangesAsync();
            }
            return true;
        }
    }
    throw new Exception("MobilePhone is null. (UpdteOrderTransferByEmail)");     
}

订单转移模型

SQL

The arguments are both string , and in SQL Server nvarchar(13) 参数都是string ,在SQL Server中为nvarchar(13)

What can cause it? 是什么原因造成的?

There's nothing wrong with your code. 您的代码没有错。 You need to double check your context whether is mapped correctly with your object. 您需要仔细检查上下文是否与对象正确映射。

To Compare string without case, use this 要比较不区分大小写的字符串,请使用此

var orderTransferFromDb = await context.OrderTransfer.FirstOrDefaultAsync(t => string.Equals(t.ToMemberMobilePhone, mobilePhone, StringComparison.OrdinalIgnoreCase));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#:无法将“System.Int64”类型的对象转换为“System.Int32”类型 - C#: Unable to cast object of type 'System.Int64' to type 'System.Int32' 无法将 System.Int64 类型的 object 转换为 System.Int32 类型 - Unable to cast object of type System.Int64 to type System.Int32 Entityframework 核心无法将类型为“System.Int32”的 object 转换为类型“System.Int64” - Entityframework Core Unable to cast object of type 'System.Int32' to type 'System.Int64' EF Core RemoveRange System.InvalidCastException:无法将“System.Int32”类型的 object 转换为“System.Int64”类型 - EF Core RemoveRange System.InvalidCastException : Unable to cast object of type 'System.Int32' to type 'System.Int64' 用于转换数值的表达式:System.InvalidCastException:&gt; 无法将“System.Int32”类型的 object 转换为“System.Int64”类型 - Expression to convert numeric values: System.InvalidCastException: > Unable to cast object of type 'System.Int32' to type 'System.Int64' 从物化的“System.Int32”类型到“System.Int64”类型的指定强制转换无效 - The specified cast from a materialized 'System.Int32' type to the 'System.Int64' type is not valid Expression.Convert:'System.Int64'类型的对象无法转换为'System.Int32'类型 - Expression.Convert: Object of type 'System.Int64' cannot be converted to type 'System.Int32' SQL和ASP.NET从实例化的“ System.Int64”类型到“ System.Int32”类型的指定强制转换无效 - SQL and ASP.NET The specified cast from a materialized 'System.Int64' type to the 'System.Int32' type is not valid 错误“无法将'System.Int32'类型的对象强制转换为'System.Collections.Generic.List`1 [System.Int32]'' - error “Unable to cast object of type 'System.Int32' to type 'System.Collections.Generic.List`1[System.Int32]'” 无法将类型为&#39;System.Data.EnumerableRowCollection`1 [System.Int32]&#39;的对象强制转换为&#39;System.IConvertible&#39; - Unable to cast object of type 'System.Data.EnumerableRowCollection`1[System.Int32]' to type 'System.IConvertible'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM