簡體   English   中英

EF Core - 無法確定導航表示的關系(一對多)

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

我正在嘗試創建一個簡單的一對多關系,但 Ef Core 不知何故無法識別它。 我仍然是這方面的初學者,但我認為我是按書本做的(完全定義了關系),所以我不明白為什么 EF Core 會拋出這個錯誤:

無法確定“保險”類型的導航“資產.保險”表示的關系。 手動配置關系,或使用“[NotMapped]”屬性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略此屬性。

這是我的兩個模型: 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; }
}

保險.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; }
}

此外,如果我刪除這種關系,只是為了測試,遷移仍然不起作用,並且會拋出有關資產 model 中其他外鍵的錯誤。 是不是因為我有太多外鍵所以我必須在 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; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM