如何修复Entity Framework中的“无法确定导航属性代表的关系”错误

时间:2019-04-11 09:28:13

标签: c# entity-framework asp.net-core

当我尝试在.NET Core 2.1网站(使用身份)上注册用户时,出现以下错误:

  

“ InvalidOperationException:无法确定由类型为'ICollection'的导航属性'City.ConnectionStartCity'表示的关系。手动配置该关系,或者使用'[NotMapped]'属性或使用'EntityTypeBuilder忽略此属性。忽略“ OnModelCreating”中的“。”

发生这种情况的原因可能与身份无关,但目前注册和登录是我知道如何触发身份的唯一方法。

我仍然希望在类中使用属​​性'City'和'ICollection',所以我不想使用'[NotMapped]'属性。

我在互联网上搜索后发现,这是由多对多关系引起的,我觉得情况并非如此。

“连接”类:

public partial class Connection
    {
        public Connection()
        {
            ConnectionRoute = new HashSet<ConnectionRoute>();
        }

        public int Id { get; set; }
        public int StartCityId { get; set; }
        public int EndCityId { get; set; }
        public int AantalMinuten { get; set; }
        public double Prijs { get; set; }

        public Stad StartCity { get; set; }
        public Stad EndCity { get; set; }
        public ICollection<ConnectionRoute> ConnectionRoute{ get; set; }
    }

“城市”类:


public partial class City
    {
        public City()
        {
            AspNetUsers = new HashSet<AspNetUsers>();
            Hotel = new HashSet<Hotel>();
            ConnectionStartCity = new HashSet<Connection>();
            ConnectionEndCity= new HashSet<Connection>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public string Country { get; set; }

        public ICollection<AspNetUsers> AspNetUsers { get; set; }
        public ICollection<Hotel> Hotel { get; set; }
        public ICollection<Connection> ConnectionStartCity { get; set; }
        public ICollection<Connection> ConnectionEndCity { get; set; }
    }

类'treinrittenContext'(dbContext)提取:

public virtual DbSet<City> City{ get; set; }

public virtual DbSet<Connection> Connection{ get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            ...

            modelBuilder.Entity<City>(entity =>
            {
                entity.Property(e => e.Country)
                    .IsRequired()
                    .HasMaxLength(255)
                    .IsUnicode(false);

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(255)
                    .IsUnicode(false);

                entity.HasMany(p => p.ConnectionStartcity)
                    .WithOne(d => d.StartCity)
                    .HasForeignKey(d => d.StartCityId);

                entity.HasMany(p => p.ConnectionEndCity)
                    .WithOne(d => d.EndCity)
                    .HasForeignKey(d => d.EndCityId);
            });

            ...

            modelBuilder.Entity<Connection>(entity =>
            {
                entity.HasOne(d => d.StartCity)
                    .WithMany(p => p.ConnectionStartCity)
                    .HasForeignKey(d => d.StartCityId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_Verbinding_BeginStad");

                entity.HasOne(d => d.EndCity)
                    .WithMany(p => p.ConnectionEndCity)
                    .HasForeignKey(d => d.EndCityId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_Verbinding_EindStad");
            });

            ...
        }

我希望这种方法能够奏效(因为在我看来这是一对多的关系),但事实并非如此。

1 个答案:

答案 0 :(得分:0)

更新

您在这里有多个选择:

选项1,结果1

enter image description here

  

城市等级变为:

public partial class City
{
    public City()
    {           
        Connections = new HashSet<Connection>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }

    public ICollection<Connection> Connections { get; set; }
}
  

连接类变为:

public partial class Connection
{
    public Connection()
    {
    }

    public int Id { get; set; }

    public int StartCityId { get; set; }
    public int EndCityId { get; set; }

    public int AantalMinuten { get; set; }
    public double Prijs { get; set; }     
}
  

您的OnModelCreating变为:

modelBuilder.Entity<City>().HasMany(city => city.Connections)
                           .WithRequired().HasForeignKey(con => con.EndCityId);

modelBuilder.Entity<City>().HasMany(city => city.Connections)
                           .WithRequired().HasForeignKey(con => con.StartCityId);

或者您也可以执行类似的操作,这将是结果2的选项2:

enter image description here

  

城市等级变为:

public partial class City
{
    public City()
    {           
        Connections = new HashSet<Connection>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }

    public ICollection<Connection> Connections { get; set; }
}
  

连接类变为:

public partial class Connection
{
    public Connection()
    {
    }

    public int Id { get; set; }

    public virtual ICollection<City> Cities { get; set; }

    public int AantalMinuten { get; set; }
    public double Prijs { get; set; }     
}

您不必在OnModelCreating中做任何事情。