c# - 从具体化 'System.Int32' 类型到 'System.Int64' 类型的指定转换无效

标签 c# asp.net-mvc entity-framework linq

在执行以下查询时,出现错误:-

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;
        }
    }

错误:

Additional information: The specified cast from a materialized 'System.Int32' type to the 'System.Int64' type is not valid.

我的模型类在这里

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; }
}

如何解决这个错误?

这是MioLMOrderConfirmAddress表的截图 MioLMOrderConfirmAddress Table

最佳答案

代码优先用户的答案...

如果您可以将值保存在“int”中,则可以通过将属性从 long 转换为 int(以及从 long? 转换为 int?)来解决此问题。

然后,在您的 OnModelCreating 方法中说明您的字段实际上是“bigint”或“decimal”:

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

如果您的数据库列是 int、bigint 或 decimal,则此方法有效。

希望这能帮助其他人解决这个问题

关于c# - 从具体化 'System.Int32' 类型到 'System.Int64' 类型的指定转换无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41693139/

相关文章:

c# - Azure BlobBatchClient.DeleteBlobsAsync 始终返回 404 blob not found

c# - 如何在 asp.net mvc 中将动态创建的字段绑定(bind)到表单提交的数组?

ASP.NET session 状态和多个工作进程

asp.net-mvc - 如何使用 Ninject 将 ModelState 作为参数注入(inject)?

.net - EntityFramework 集合属性是否需要 setter

asp.net - 使用内存对 .ToListAsync() 进行单元测试

c# - 如何在 Linq 中组合多个相同或不同长度的列表?

c# - Azure MobileApp 自定义身份验证 VS Asp.Net 身份

c# - 全局鼠标事件

c# - EntityFramework 中的 TransactionScope TransactionInDoubtException