weg*_*rer 7 entity-framework entity-framework-core ef-fluent-api .net-core entity-framework-core-2.2
我一直收到以下错误消息,但我似乎无法理解我收到它的原因。有趣的是,在添加迁移时我没有收到任何错误,但是每当我想使用上下文时,我都会得到它。
无法确定“Block.JobBlock”和“JobBlock.Block”之间的一对一关系的子/依赖方。要识别关系的子/依赖方,请配置外键属性。如果这些导航不应该是同一关系的一部分,请在不指定反向的情况下配置它们。
AJob可以有多个JobBlocks(一对多);singleBlock只能有一个JobBlock(一对一)。所以基本上, aJobBlock是一个参考表/实体,用于引用Job及其Blocks. 值得一提的是,JobBlock实体中的主键由两个键组成,因此成为复合主键。
有人可能会争辩说,一个Block实体应该已经包含一个IdJob属性,并且该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; }
}
Run Code Online (Sandbox Code Playgroud)
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 });
}
}
Run Code Online (Sandbox Code Playgroud)
问题出在您的Block和JobBlock配置中。根据您的要求,这两种配置应如下所示:
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.
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7750 次 |
| 最近记录: |