I've been getting the following error message and I can't seem to grasp the reason why have I been getting it. Interestingly when adding migrations I didn't get any errors, but whenever I want to use the context I do get it.
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.
A Job
can have multiple JobBlocks
(one-to-many); single Block
can have only one JobBlock
(one-to-one). So basically, a JobBlock
is a reference table/entity used to reference Job
and its Blocks
. It is important to mention that a primary key in the JobBlock
entity consists of two keys thus making it a compound primary key.
One might argue that a Block
entity should already contain an IdJob
property and that the JobBlock
entity could be entirely dismissed, but there is some reasoning why it shouldn't be done this way so let's leave it as it is :)
Models:
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 Configurations:
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 });
}
}