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

okn*_*syl 3 c# entity-framework entity-framework-core asp.net-core

我正在尝试创建一个简单的一对多关系,但 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; }
}
Run Code Online (Sandbox Code Playgroud)

保险.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; }
}
Run Code Online (Sandbox Code Playgroud)

另外,如果我删除这种关系,只是为了测试,迁移仍然不起作用,并会抛出有关资产模型中其他外键的错误。是因为我有太多外键所以我必须在 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)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

Ric*_*ing 5

如果指定[ForeignKey]属性,则需要执行以下两项操作之一:

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

所以要么:

[ForeignKey("Insurance")]
public int InsuranceId { get; set; }
public Insurance Insurance { get; set; }
Run Code Online (Sandbox Code Playgroud)

或者:

public int InsuranceId { get; set; }
[ForeignKey("InsuranceId")]
public Insurance Insurance { get; set; }
Run Code Online (Sandbox Code Playgroud)

通过将属性放在标量属性上并指定标量属性的名称,EF 无法理解您要执行的操作。

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

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


编辑:

该实体有两个导航属性Insurance

public Insurance Insurance { get; set; }
...
public Insurance Invoice { get; set; }
Run Code Online (Sandbox Code Playgroud)

我怀疑第二个应该是:

public Invoice Invoice { get; set; }
Run Code Online (Sandbox Code Playgroud)

  • 这就好像您正在尝试为不属于模型一部分的实体定义导航属性。您的“DbContext”上是否有“DbSet&lt;Insurance&gt;”属性? (2认同)