EF指定的架构无效。错误: 无法加载关系x,因为类型x不可用
今天在一个表对应两个外键报错:指定的架构无效。错误: 无法加载关系x,因为类型x不可用.
如果是第一个外键,那就是正常的写法,第二个开始用数字12345依次开始.
直接上代码,
Model
public partial class Teams { public int team_id { get; set; } public string team_name { get; set; } public virtual ICollection<Matches> Matches { get; set; } public virtual ICollection<Matches> Matches1 { get; set; } } public partial class Matches { public int match_id { get; set; } public int team_A_id { get; set; } public int team_B_id { get; set; } public virtual Teams Teams { get; set; } public virtual Teams Teams1 { get; set; } }
DbContext关系对应写法:
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Matches>().HasKey(n => n.match_id); modelBuilder.Entity<Matches>().Property(n => n.match_id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); modelBuilder.Entity<Matches>().HasRequired(n => n.Teams).WithMany(n => n.Matches).HasForeignKey(n => n.team_A_id); modelBuilder.Entity<Matches>().HasRequired(n => n.Teams1).WithMany(n => n.Matches1).HasForeignKey(n => n.team_B_id); base.OnModelCreating(modelBuilder); }
按照上面这写法应该是没有问题的.
以上就是【EF指定的架构无效。错误: 无法加载关系x,因为类型x不可用】的全部内容了,欢迎留言评论进行交流!