[英]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.