Find' 的类型为 'int',与 'long' 的属性类型不匹配

问题描述 投票:0回答:1

实体。

[表(“订单行”,架构 =“销售”)]

    public class OrderLine
    {
        [Key]
        public long OrderLineID { get; set; }
    }

尝试通过ID获取实体

public virtual async Task<T> GetByIdAsync(object id)
{
    if (id == null) throw new ArgumentNullException("Identifier is null");
    return await _entities.FindAsync(id);
}

出现错误 System.AggregateException:“发生一个或多个错误。(调用“DbSet.Find”的位置 0 处的键值是“int”类型,与“long”的属性类型不匹配。)”

ef-core-2.0
1个回答
0
投票

不确定您是否对此有解决方案,但我遇到了同样的问题,这是我的问题和解决方案。

我有一个很久以前搭建的上下文,需要更新。在生成新的脚手架上下文后,我的方法停止工作。我发现,尽管我们在两者中都提供了所有属性,但新上下文更改了我的表类的 modelBuilder.Entity 中

keys
的顺序。

所以我停止工作的方法是

if(await _ctx.MyTbl.FindAsync(a,b,c))
    DoSomething();

旁注:我的表是一个 db2 表,有 3 个主键

我的旧 modelBuilder.Entity 看起来像

modelBuilder.Entity<MarketingRepresentativeAssignmentHistory_Ta00178>(entity =>
{
    entity.HasKey(e => new { a, b, c }).HasName("MyKey");
}

我的新的看起来像

modelBuilder.Entity<MarketingRepresentativeAssignmentHistory_Ta00178>(entity =>
{
    entity.HasKey(e => new { b, a, c }).HasName("MyKey");
}

问题是

a
是 DateTime 类型,而
b
是字符串类型。

通过更改新上下文中的顺序,参数现在乱序,并尝试将错误的类型传递给数据库期望的类型。

实际上,它现在完全有意义,因为

FindAsync
是一种方法,参数必须按顺序排列,除非指定了参数名称,但我没有找到任何特别提到顺序重要的内容。

© www.soinside.com 2019 - 2024. All rights reserved.