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); } }