EF Core - 无法确定导航所代表的关系(一对多)

EF Core - Unable to determine the relationship represented by navigation (One-to-Many)

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

Unable to determine the relationship represented by navigation 'Asset.Insurance' of type 'Insurance'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

这是我的两个模型: Asset.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)
    {
    }
}

如果指定 [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] 属性。


编辑:

Insurance 实体有两个导航属性:

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

我怀疑第二个应该是:

public Invoice Invoice { get; set; }