与导航 属性 的一对多关系仅在一侧?

One-To-Many relationship with navigation property only on one side?

我在两个表之间有完美的一对多关系:PlayPlayer:

public class Player
{
    public int PlayerId { get; set; }
    public string Name { get; set; }

    public ICollection<Play> Plays { get; set; }
}

public class Play
{
    public int PlayId { get; set; }

    public int PlayerId { get; set; }
    public Player Player { get; set; }
}

modelBuilder.Entity<Play>()
    .HasOne<Player>(p => p.Player)
    .WithMany(p => p.Plays)
    .OnDelete(DeleteBehavior.Cascade)
    .HasForeignKey(p => p.PlayerId)
    .IsRequired();

是否可以在旁边省略导航 属性?例如从 Player 中删除 Plays 集合?

public class Player
{
    public int PlayerId { get; set; }
    public string Name { get; set; }
}

我有两个具体问题:

  1. 有可能做到吗?
  2. 是否可以在不依赖 EF Core 约定的情况下做到这一点 - 例如"Id" 是主键等等...? (意思是,让一切都明确,无论是流利的 API 还是作为注释)

Is it possible to omit navigation property on side? For example remove Plays collection from Player?

绝对有可能。写你的实体配置如下:

modelBuilder.Entity<Play>()
    .HasOne<Player>(p => p.Player)
    .WithMany() // <-- Here it is
    .HasForeignKey(p => p.PlayerId)
    .OnDelete(DeleteBehavior.Cascade)
    .IsRequired();

Is it possible to do it, without relying on EF Core conventions - e.g. "Id" is a primary key etc...? (Meaning, having everything explicit, either in fluent API or as Annotations)

不!没有 Fluent API.

是不可能的