【问题标题】:EF Core - Unable to determine the relationship represented by navigation (One-to-Many)EF Core - 无法确定导航表示的关系(一对多)
【发布时间】:2022-01-14 01:33:49
【问题描述】:

我正在尝试创建一个简单的一对多关系,但 Ef Core 不知何故无法识别它。我仍然是这方面的初学者,但我想我是按书本做的(完全定义了关系),所以我不明白为什么 EF Core 会抛出这个错误:

无法确定导航所代表的关系 'Insurance' 类型的'Asset.Insurance'。要么手动配置 关系,或使用“[NotMapped]”忽略此属性 属性或在“OnModelCreating”中使用“EntityTypeBuilder.Ignore”。

这是我的两个模型: 资产.cs

public class Asset
{
    public int AssetId { get; set; }
    public string AssetName { get; set; }

    [ForeignKey("AssetTypeId")]
    public int AssetTypeId { get; set; }
    public AssetType AssetType { get; set; }
    
    public ICollection<AssetValue> AssetTypeValues { get; set; }
    
    public string Brand { get; set; }
    public string Model { get; set; }
    public string SerialNumber { get; set; }
    public string ProductNumber { get; set; }
    public DateTime PurchaseDate { get; set; }
    public decimal PurchasePrice { get; set; }
    
    [ForeignKey("LocationId")]
    public int? LocationId { get; set; }
    public Location Location { get; set; }

    [ForeignKey("InsuranceId")]
    public int InsuranceId { get; set; }
    public Insurance Insurance { get; set; }

    [ForeignKey("OwnerId")]
    public string? OwnerId { get; set; }
    public AppUser Owner { get; set; }

    [ForeignKey("InvoiceId")]
    public int? InvoiceId { get; set; }
    public Insurance Invoice { get; set; }

    public string? ImagePath { get; set; }
    public string? Description { get; set; }
    public string CreatedBy { get; set; }
    public string ModifiedBy { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime DateModified { get; set; }
}

Insurance.cs

public class Insurance
{
    public int InsuranceId { get; set; }

    [ForeignKey("InsuranceTypeId")]
    public int InsuranceTypeId { get; set; }
    public InsuranceType InsuranceType { get; set; }

    public string Insurer { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public string? FilePath { get; set; }
    public string? Description { get; set; }
    public string CreatedBy { get; set; }
    public string ModifiedBy { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime DateModified { get; set; }

    public ICollection<Asset> Assets { get; set; }
}

另外,如果我删除这个关系,只是为了测试,迁移仍然不起作用,并且会抛出有关资产模型中其他外键的错误。是不是因为我的外键太多,所以必须在 OnModelCreating 中定义?

编辑:ApplicationDbContext

public class ApplicationDbContext : IdentityDbContext<AppUser>
{
    public DbSet<Asset> Assets { get; set; }
    public DbSet<AssetType> AssetTypes { get; set; }
    public DbSet<AssetProp> AssetProps { get; set; }
    public DbSet<AssetValue> AssetValues { get; set; }
    public DbSet<Location> Locations { get; set; }
    public DbSet<Company> Companies { get; set; }
    public DbSet<CompanyContact> CompanyContacts { get; set; }
    public DbSet<Invoice> Invoices { get; set; }
    public DbSet<InsuranceType> InsuranceTypes { get; set; }
    public DbSet<Insurance> Insurances { get; set; }

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
}

【问题讨论】:

    标签: c# entity-framework asp.net-core entity-framework-core


    【解决方案1】:

    如果您指定[ForeignKey] 属性,您需要做以下两件事之一:

    • 将属性添加到标量属性,并使用导航属性的名称;或
    • 将属性添加到导航属性,并使用标量属性的名称。

    所以要么:

    [ForeignKey("Insurance")]
    public int InsuranceId { get; set; }
    public Insurance Insurance { get; set; }
    

    或:

    public int InsuranceId { get; set; }
    [ForeignKey("InsuranceId")]
    public Insurance Insurance { get; set; }
    

    通过将属性放在标量属性上并指定标量属性的名称,EF 无法理解您要做什么。

    这适用于代码中的所有[ForeignKey("...")] 属性。

    注意:由于每个给定实体类型只有一个导航属性,并且标量属性名称与添加了 Id 后缀的导航属性名称相匹配,因此 [ForeignKey] 属性不是t 实际上是必需的。


    编辑:

    Insurance 实体有两个导航属性:

    public Insurance Insurance { get; set; }
    ...
    public Insurance Invoice { get; set; }
    

    我怀疑第二个应该是:

    public Invoice Invoice { get; set; }
    

    【讨论】:

    • 我不知道,谢谢!但我认为我还缺少其他一些东西/错误,因为我已经尝试了所有三个选项(包括删除 [ForeignKey] 属性)但仍然是同样的错误。
    • 就好像您正在尝试为不属于您的模型的实体定义导航属性。你的DbContext 上有DbSet&lt;Insurance&gt; 属性吗?
    • 不,顺序无关紧要。我想我已经发现了问题......
    • @oknsyl 您在Invoice 导航属性上使用了错误的类型。我已经更新了我的答案。 :)
    • @oknsyl 没问题 - 这是一个容易忽略的错误,并且错误消息并不是特别有用。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 2019-08-22
    • 1970-01-01
    • 2021-12-29
    • 2020-09-03
    相关资源
    最近更新 更多