简体   繁体   English

从物化的“System.Int32”类型到“System.Int64”类型的指定强制转换无效

[英]The specified cast from a materialized 'System.Int32' type to the 'System.Int64' type is not valid

On executing following query, I get the error :- 在执行以下查询时,我收到错误: -

public MioLMOrderConfirmAddress GetAddress(long headerId,int addressCategory)
    {
        using (var c = new TenantEntities(_tenantConString))
        {
            var data =
                c.MioLMOrderConfirmAddresses.FirstOrDefault(
                    x => x.MioLMOrderConfirmHeaderId == headerId && x.AddressCategoryId == addressCategory);
            return data;
        }
    }

Error : 错误:

Additional information: The specified cast from a materialized 'System.Int32' type to the 'System.Int64' type is not valid. 附加信息:从物化的“System.Int32”类型到“System.Int64”类型的指定强制转换无效。

My Model class is here 我的Model类在这里

public partial class MioLMOrderConfirmAddress
{
    public long Id { get; set; }
    public long MioLMOrderConfirmHeaderId { get; set; }
    public Nullable<long> MioLMOrderConfirmLineId { get; set; }
    public int AddressCategoryId { get; set; }
    public string FullAddress { get; set; }
    public string StreetName { get; set; }
    public string AdditionalStreetName { get; set; }
    public string CityName { get; set; }
    public string PostalZone { get; set; }
    public string CountrySubEntity { get; set; }
    public string CountryCode { get; set; }
    public string BuildingNumber { get; set; }
    public string AddressFormatCode { get; set; }
    public string AddressTypeCode { get; set; }
    public string BlockName { get; set; }
    public string BuildingName { get; set; }
    public string CitySubDivisionName { get; set; }
}

How to solve this error? 如何解决这个错误?

This is the screenshot of MioLMOrderConfirmAddress table MioLMOrderConfirmAddress Table 这是MioLMOrderConfirmAddress表MioLMOrderConfirmAddress表的屏幕截图

Answer for code-first users... 代码优先用户的答案......

If you can hold your values inside an "int", you can work around this problem by converting your properties from long to int (and from long? to int?). 如果您可以将值保存在“int”中,则可以通过将属性从long转换为int(以及从long转换为int?)来解决此问题。

Then, tell that your fields are in fact "bigint" or "decimal" in your OnModelCreating method: 然后,告诉您的字段实际上是OnModelCreating方法中的“bigint”或“decimal”:

modelBuilder
    .Properties()
    .Where(p => p.Name.EndsWith("Id"))
    .Configure(c => c.HasColumnType("bigint"));

This works if your database columns are either int, bigint or decimal. 如果您的数据库列是int,bigint或decimal,则此方法有效。

Hope this helps someone else with this issue 希望这可以帮助其他人解决这个问题

暂无
暂无

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

相关问题 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.String”类型的指定强制转换无效 - The specified cast from a materialized 'System.Int32' type to the 'System.String' type is not valid {“从物化'System.Guid'类型到'System.Int32'类型的指定强制转换无效。” - {“The specified cast from a materialized 'System.Guid' type to the 'System.Int32' type is not valid.” System.InvalidOperationException:从实例化的“ System.Int32”类型到可为空的“ Country”类型的指定强制转换无效 - System.InvalidOperationException: The specified cast from a materialized 'System.Int32' type to a nullable 'Country' type is not valid 从具体化的“System.Int32”类型到“System.Double”类型的指定转换无效 - The specified cast from a materialized 'System.Int32' type to the 'System.Double' type is not valid 从具体化的“System.Double”类型到“System.Int32”类型的指定转换无效但没有定义 Double - The specified cast from a materialized 'System.Double' type to the 'System.Int32' type is not valid But no Double defined 从实例化的“ System.Guid”类型到“ System.Int32”类型的指定强制转换无效 - The specified cast from a materialized 'System.Guid' type to the 'System.Int32' type is not valid 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'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM