构建迁移时,出现以下错误:
无法确定“Location”类型的导航属性“Location.NorthLocation”表示的关系。手动配置关系,或从模型中忽略此属性。
位置实体:
public class Location
{
public Guid Id { get; set; }
public DateTime CreatedWhen { get; set; }
public string CreatedBy { get; set; }
public DateTime ModifiedWhen { get; set; }
public string ModifiedBy { get; set; }
public Guid? NorthLocationId { get; set; }
public virtual Location NorthLocation { get; set; }
public Guid? SouthLocationId { get; set; }
public virtual Location SouthLocation { get; set; }
public Guid? EastLocationId { get; set; }
public virtual Location EastLocation { get; set; }
public Guid? WestLocationId { get; set; }
public virtual Location WestLocation { get; set; }
}
类型配置:
public class MyContext : DbContext
{
public MyContext(DbContextOptions<MyContext> options) : base(options)
{
}
public DbSet<Location> Locations { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<T>().HasKey("Id");
builder.Entity<T>().Property("Id").ValueGeneratedOnAdd();
builder.Entity<T>().Property("CreatedWhen").HasDefaultValueSql("GETDATE()").ValueGeneratedOnAdd();
builder.Entity<T>().Property("ModifiedWhen").IsRequired();
builder.Entity<T>().Property("CreatedBy").HasMaxLength(50).IsRequired();
builder.Entity<T>().Property("ModifiedBy").HasMaxLength(50).IsRequired();
// Locations
builder.Entity<Location>().HasOne(x => x.NorthLocation).WithOne(x => x.SouthLocation).HasForeignKey(typeof(Location), "NorthLocationId").OnDelete(DeleteBehavior.SetNull);
builder.Entity<Location>().HasOne(x => x.SouthLocation).WithOne(x => x.NorthLocation).HasForeignKey(typeof(Location), "SouthLocationId").OnDelete(DeleteBehavior.SetNull);
builder.Entity<Location>().HasOne(x => x.EastLocation).WithOne(x => x.WestLocation).HasForeignKey(typeof(Location), "EastLocationId").OnDelete(DeleteBehavior.SetNull);
builder.Entity<Location>().HasOne(x => x.WestLocation).WithOne(x => x.EastLocation).HasForeignKey(typeof(Location), "WestLocationId").OnDelete(DeleteBehavior.SetNull);
}
}
我的目标是拥有一个 Location 实体,它可以自引用它自己的北/南/东/西邻居。
谁能建议我为什么会收到此错误?