在执行以下查询时,出现错误:-
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/