在构建迁移我收到以下错误:实体框架的核心一对一的自参照关系失败
无法确定按类型“位置”的导航属性“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);
}
}
我的目标是有一个自我引用它自己的邻居到北部一个位置实体/南/东/西。
任何人都可以提出为什么我可能会得到这个错误?
如果这两行被排除在外如何处理相应的外键我会属性?是否足够聪明地弄清楚并填充它们呢? – Hades
您只能在数据库中为每个南北和每个东西方关系确实拥有一个FK。选择何处定位FK(例如,北或南行)是完全任意的。这反过来又是一个关系数据库设计问题,而不是EF核心问题。顺便说一下,我还没有提到它,但我不确定跟踪南北和东西方关系是模拟实际地理分布位置的好方法。 – divega
我尝试删除这两行,它仍然为我引发相同的错误。这是一个战略游戏的空间地图。基本上我想有能力在任何方向“旅行”。 – Hades