entity-framework - EF Core 中的一对一关系(无法确定一对一关系的子/依赖方)

标签 entity-framework .net-core entity-framework-core ef-fluent-api entity-framework-core-2.2

我一直收到以下错误消息,但似乎无法理解我收到它的原因。有趣的是,在添加迁移时我没有收到任何错误,但是每当我想使用上下文时,我都会得到它。

The child/dependent side could not be determined for the one-to-one relationship between 'Block.JobBlock' and 'JobBlock.Block'. 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.



一个 Job可以有多个 JobBlocks (一对多);单例Block只能有一个 JobBlock (一对一)。所以基本上,一个 JobBlock是用于引用 Job 的引用表/实体和它的 Blocks .重要的是要提到 JobBlock 中的主键实体由两个键组成,从而使其成为复合主键。

有人可能会争辩说 Block实体应该已经包含 IdJob属性(property)和JobBlock实体可以完全被驳回,但有一些理由为什么不应该这样做,所以让我们保持原样:)

型号:
public class Job : IEntity
{
    public Job()
    {
        JobBlocks = new HashSet<JobBlock>();
    }

    public Guid Id { get; set; } = Guid.NewGuid();
    public ICollection<JobBlock> JobBlocks { get; set; }
}

public class Block : IEntity
{
    public Guid Id { get; set; } = Guid.NewGuid();
    public JobBlock JobBlock { get; set; }
}


public class JobBlock : IEntity
{
    public Guid IdJob { get; set; }
    public Job Job { get; set; }

    public Guid IdBlock { get; set; }
    public Block Block { get; set; }
}

EF 配置:
public class JobConfiguration : IEntityTypeConfiguration<Job>
{
    public void Configure(EntityTypeBuilder<Job> builder)
    {
        builder.HasKey(p => p.Id);
        builder.Property(p => p.Id) .IsRequired() .ValueGeneratedNever();

        builder.HasMany(e => e.JobBlocks)
            .WithOne(e => e.Job)
            .HasForeignKey(p => p.IdJob);
    }
}

public class BlockConfiguration : IEntityTypeConfiguration<Block>
{
    public void Configure(EntityTypeBuilder<Block> builder)
    {
        builder.HasKey(p => p.Id);
        builder.Property(p => p.Id).IsRequired().ValueGeneratedNever();

        builder.HasOne(e => e.JobBlock)
            .WithOne(e => e.Block)
            .HasForeignKey<JobBlock>(p => new { p.IdJob, p.IdBlock });
    }
}

public class JobBlockConfiguration : IEntityTypeConfiguration<JobBlock>
{
    public void Configure(EntityTypeBuilder<JobBlock> builder)
    {
        builder.HasKey(p => new { p.IdJob, p.IdBlock });
        builder.Property(p => p.IdJob).IsRequired();
        builder.Property(p => p.IdBlock).IsRequired();

        builder.HasOne(e => e.Job)
            .WithMany(e => e.JobBlocks)
            .HasForeignKey(p => p.IdJob);

        builder.HasOne(e => e.Block)
            .WithOne(e => e.JobBlock)
            .HasForeignKey<JobBlock>(p => new { p.IdJob, p.IdBlock });
    }
}

最佳答案

问题出在您的 BlockJobBlock配置。根据您的要求,这两种配置应如下所示:

public class BlockConfiguration : IEntityTypeConfiguration<Block>
{
    public void Configure(EntityTypeBuilder<Block> builder)
    {
        builder.HasKey(p => p.Id);
        builder.Property(p => p.Id).IsRequired().ValueGeneratedNever();

        builder.HasOne(e => e.JobBlock)
            .WithOne(e => e.Block)
            .HasForeignKey<JobBlock>(p => p.IdBlock); // <--- Here it is
    }
}
public class JobBlockConfiguration : IEntityTypeConfiguration<JobBlock>
{
    public void Configure(EntityTypeBuilder<JobBlock> builder)
    {
        builder.HasKey(p => new { p.IdJob, p.IdBlock });

        // Key property is always required. You don't need to specify it explicitly.

        // You don't need to need specify one-one-one configuration
        //  between `Job and Block` and between `Block and JobBlock` in
        //  two places. You need to specify
        //  it only one place. That's why I have removed these from here.
    }
}

关于entity-framework - EF Core 中的一对一关系(无法确定一对一关系的子/依赖方),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55454404/

相关文章:

c# - SQLite + EntityFramework 对在处理上下文后使连接保持事件状态

asp.net-mvc - 如果我的字符串列表类似于实体列,则显示调用

asp.net-mvc - AJAX Get 请求返回成功或错误,具体取决于从 Controller 接收到的对象

c# - 具有 Entity Framework 的枚举 - 独立于表行 ID 的枚举值

c# - 如何在 .NET Core 上注册新的 NuGet 包源?

c# - 将 .NET Core 应用发布为可移植的可执行文件

docker - 如何为引用兄弟目录的项目构建 Dockerfile?

c# - EF Core 1.1 迁移 - 当前的 CSharpHelper 无法构建类型文字

entity-framework - 保存时EF Core OwnsOne失败

c# - EFCore SqlClient 错误 LocalDB