繁体   English   中英

EF Core 中的一对一关系会导致子/依赖异常

[英]One-to-one relationship in EF Core gives child/dependent exception

我有两个实体代表 Web 应用程序的用户,以及 Web 应用程序所涉及的播客中的参与者。 网页的用户有个人资料,可以登录,可以发表评论等。参与者从他们出现的剧集对象链接,有个人简介,图片等。可以既是参与者又是用户同时,但也可以只是一个用户,或者只是一个参与者。

我正在努力在 EF Core 3.1 中对此进行建模。 如果重要的话,我也在这个项目中使用 .Net Core 3.0,数据库是 Postgresql(使用 Nuget 包 Npgsql.EntityFrameworkCore.PostgreSQL v3.1.0)。

在双方,这种关系应该可以为空/不需要。 实体非常简单(省略了所有非重要属性):

用户:

public class User
{
    public Guid UserId { get; set; }
    public Participant Participant { get; set; }

    public Guid ParticipantId { get; set; }
}

参与者:

public class Participant
{
    public Guid ParticipantId { get; set; }

    public User User { get; set; }

    public Guid UserId { get; set; }

}

我正在尝试使用 Fluent API 来配置关系 - 这似乎是它崩溃的地方。

用户配置:

public class UserConfiguration : IEntityTypeConfiguration<User>
{
    public void Configure(EntityTypeBuilder<User> user)
    {
        user.ToTable("users");

        user.HasKey(u => u.UserId);

        user.HasOne(u => u.Participant)
            .WithOne(p => p.User)
            .HasForeignKey<Participant>(p => p.UserId);

    }
}

参与者配置:

public class ParticipantConfiguration : IEntityTypeConfiguration<Participant>
{
    public void Configure(EntityTypeBuilder<Participant> participant)
    {
        participant.ToTable("participants");

        participant.HasKey(p => p.ParticipantId);

        participant.HasOne<User>(p => p.User)
            .WithOne(u => u.Participant)
            .HasForeignKey<User>(u => u.ParticipantId);
    }
}

现在我意识到你应该只配置关系的一侧 - 至少我是这样解释我所读的内容的。 为了完整起见,我刚刚包括了上述内容; 我已经尝试像上面那样同时配置双方,我尝试只在用户端和参与者端进行配置。 在每种组合中,代码都会编译,应用程序会启动,但是当我尝试通过 DbContext 实际将用户添加到数据库时,我得到了相同的异常:

System.InvalidOperationException: 'The child/dependent side could not be determined for the one-to-one relationship between 'Participant.User' and 'User.Participant'. To identify the child/dependent side of the relationship, configure the foreign key property. If these navigations should not be part of the same relationship configure them without specifying the inverse. See http://go.microsoft.com/fwlink/?LinkId=724062 for more details.'

现在,这是两个完全独立的对象,碰巧彼此认识,因此我不确定 child/dependent 的心态是否准确,但我愿意忽略该细节以屈服于 EF Core 的意愿; 但是,我不知道如何让我毫无例外地工作。

特尔;博士:

  • 用户可以有一个参与者链接。
  • 参与者可以有一个用户链接。
  • 该链接为 NULL 是完全没问题的。
  • 我如何为此配置 EF Core?

感谢您的任何见解!

我认为要做的第一件事是将外键更改为可空值。

public class User
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key]
    public Guid UserId { get; set; }

    public Participant Participant { get; set; }

    public Guid? ParticipantId { get; set; }
}

public class Participant
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key]
    public Guid ParticipantId { get; set; }

    public User User { get; set; }

    public Guid? UserId { get; set; }
}

然后保持您的配置完好无损。 我的示例工作配置:

 protected override void OnModelCreating(ModelBuilder modelBuilder)
 {
            modelBuilder.Entity<User>().HasOne(t => t.Participant)
                     .WithOne(t => t.User)
                      .HasForeignKey<Participant>(t => t.UserId);

            modelBuilder.Entity<Participant>().HasOne(t => t.User)
                     .WithOne(t => t.Participant)
                     .HasForeignKey<User>(t => t.ParticipantId);
            base.OnModelCreating(modelBuilder);
 }

我会仔细检查您的配置是否被调用。 一切看起来都应该有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM