如何修复实体框架中的“无法确定导航属性表示的关系”错误

Luk*_*Nys 14 c# entity-framework asp.net-core

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

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

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

我仍然希望在我的类中使用属​​性 'City' en '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; }
    }

Run Code Online (Sandbox Code Playgroud)

“城市”类:


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; }
    }
Run Code Online (Sandbox Code Playgroud)

类 '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");
            });

            ...
        }
Run Code Online (Sandbox Code Playgroud)

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

Dim*_*tri 16

更新

您在这里有多种选择:

选项 1 结果为 1

在此处输入图片说明

城市类变为:

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; }
}
Run Code Online (Sandbox Code Playgroud)

连接类变为:

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; }     
}
Run Code Online (Sandbox Code Playgroud)

您的 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);
Run Code Online (Sandbox Code Playgroud)

或者你也可以做这样的事情,这将是结果 2 的选项 2:

在此处输入图片说明

城市类变为:

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; }
}
Run Code Online (Sandbox Code Playgroud)

连接类变为:

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; }     
}
Run Code Online (Sandbox Code Playgroud)

而且您无需在 OnModelCreating 中执行任何操作。