学海无涯

导航

EFCore 迁移异常解决方案

添加迁移时显示错误:

Both relationships between 'WorkCenter.Factory' and 'Factory' and between 'WorkCenter' and 'Factory.WorkCenters' could use {'FactoryId'} as the foreign key. To resolve this, configure the foreign key properties explicitly in 'OnModelCreating' on at least one of the relationships.

解决方案:

namespace APS.Infrastructure.Data.Config.BasicData;
public class FactoryConfig : IEntityTypeConfiguration<Factory>
{
  public void Configure(EntityTypeBuilder<Factory> builder)
  {
    builder.HasOne(m => m.Organization).WithMany().OnDelete(DeleteBehavior.NoAction);
    builder.HasIndex(m => m.Code).IsUnique();

    builder.HasMany(m => m.WorkCenters).WithOne(m => m.Factory).HasForeignKey(m => m.FactoryId)
     .HasPrincipalKey(m => m.Id);
  }
}

  

 

posted on 2024-05-06 11:33  宁静致远.  阅读(21)  评论(0编辑  收藏  举报