从具体化的“System.Int32”类型到“System.Int64”类型的指定转换无效

NHK*_*NHK 5 c# linq asp.net-mvc entity-framework

在执行以下查询时,我收到错误:-

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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

错误 :

附加信息:从具体化的“System.Int32”类型到“System.Int64”类型的指定转换无效。

我的模型课在这里

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; }
}
Run Code Online (Sandbox Code Playgroud)

如何解决这个错误?

这是 MioLMOrderConfirmAddress 表MioLMOrderConfirmAddress 表的截图

jBe*_*ger 0

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

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

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

modelBuilder
    .Properties()
    .Where(p => p.Name.EndsWith("Id"))
    .Configure(c => c.HasColumnType("bigint"));
Run Code Online (Sandbox Code Playgroud)

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

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