如何修复Entity Framework中的“无法确定导航属性所代表的关系"错误 [英] How to fix 'Unable to determine the relationship represented by navigation property' error in Entity Framework

查看:82
本文介绍了如何修复Entity Framework中的“无法确定导航属性所代表的关系"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I try to register a user on my .NET Core 2.1 website (using identity) I get the following error:

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

"InvalidOperationException: Unable to determine the relationship represented by navigation property 'City.ConnectionStartCity' of type 'ICollection'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.".

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

The reason this happens probably has nothing to do with identity, but registering and logging in is currently the only way I know how to trigger it.

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

I still want the properties 'City" en 'ICollection' in my classes so I don't want to use the '[NotMapped]' attribute.

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

I searched on the internet and found that this is caused by a many-many relationship, I feel like this is not the case tho.

连接"类:

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)提取:

Class 'treinrittenContext' (dbContext) extract:

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

            ...
        }

我希望这种方法行之有效(因为在我看来这是一对多的关系),但事实并非如此.

I expect this to work (since in my eyes it's a one-many relation), but it doesn't.

推荐答案

更新

您在这里有多种选择:

选项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; }
}

连接类变为:

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变为:

Your OnModelCreating becomes:

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:

城市等级变为:

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中做任何事情.

And you don't have to do anything in your OnModelCreating.

这篇关于如何修复Entity Framework中的“无法确定导航属性所代表的关系"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆