gpt4 book ai didi

c# - 如何修复 Entity Framework 中的 'Unable to determine the relationship represented by navigation property' 错误

转载 作者:行者123 更新时间:2023-12-03 15:40:54 25 4
gpt4 key购买 nike

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

"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'.".



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

我仍然希望我的类中的属性 '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; }
}


“城市”类:

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 结果为 1

enter image description here

City Class becomes:


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

Connection Class becomes:


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

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:

enter image description here

City Class becomes:


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

Connection Class becomes:


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 中执行任何操作。

关于c# - 如何修复 Entity Framework 中的 'Unable to determine the relationship represented by navigation property' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55629131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com