EF Core 6 – 在dbContext中创建关系时出错

huangapple go评论81阅读模式
英文:

EF Core 6 - Error when creating relationship in dbContext

问题

以下是要翻译的内容:

这是我需要创建的关系
this Relation

这是每个表的实体类

机场表:

[Table("Airport")]
public class Airport
{
[Key]
public Guid Id { get; set; }
[Required]
[MaxLength(50)]
public string Name { get; set; }
[Required]
[MaxLength(50)]
public string City { get; set; }
[Required]
[MaxLength(50)]
public string Country { get; set; }
public ICollection DepartureAirports { get; set; }
public ICollection ArrivalAirports { get; set; }
public Airport()
{
DepartureAirports = new List();
ArrivalAirports = a new List();
}
}

航班时间表:

[Table("FlightSchedule")]
public class FlightSchedule
{
[Key]
public Guid Id { get; set; }
[Required]
public Guid DepartureAirportId { get; set; }
public Airport AirportDepart { get; set; }
[Required]
public Guid ArrivalAirportId { get; set; }
public Airport AirportArrival { get; set; }
[Required]
public DateTime DepartureTime { get; set; }
[Required]
public DateTime ArrivalTime { get; set; }
public ICollection Flights { get; set; }
public FlightSchedule()
{
Flights = a new List();
}
}

这是我的DbContext配置在OnModelCreating()方法中:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity(entity =>
{
entity.Property(e => e.Id).ValueGeneratedOnAdd();
});

modelBuilder.Entity<FlightSchedule>(entity =>
{
    entity.Property(e => e.Id).ValueGeneratedOnAdd();

    entity.HasOne(e => e.AirportDepart)
    .WithMany(e => e.DepartureAirports)
    .HasForeignKey(e => e.DepartureAirportId)
    .OnDelete(DeleteBehavior.ClientSetNull);

    entity.HasOne(e => e.AirportArrival)
        .WithMany(e => e.ArrivalAirports)
        .HasForeignKey(e => e.ArrivalAirportId)
      .OnDelete(DeleteBehavior.ClientSetNull);
});

}

从SQL Server生成的图表与我的设计相符,但当我为这两个表添加一些数据种子时,只有机场起作用,航班时间表会产生以下错误

由于一个实体类型上存在多个属性 - {'ArrivalAirports','DepartureAirports'},可以与其他实体类型上的属性匹配 - {'AirportArrival','AirportDepart'},所以按照约定未配置从'Airport'到'FlightSchedule'的关系。如果在'OnModelCreating'中指定了显式配置,则可以忽略此消息。

我怀疑可能是我的配置有问题,但我不确定是什么原因,因为这是我第一次尝试使用Code First。

英文:

Here is the relationship I need to create
this Relation

And here are the entity classes for each table

The Airport table:

[Table(&quot;Airport&quot;)]
public class Airport
{
    [Key]
    public Guid Id { get; set; }
    [Required]
    [MaxLength(50)]
    public string Name { get; set; }
    [Required]
    [MaxLength(50)]
    public string City { get; set; }
    [Required]
    [MaxLength(50)]
    public string Country { get; set; }
    public ICollection&lt;FlightSchedule&gt; DepartureAirports { get; set; }
    public ICollection&lt;FlightSchedule&gt; ArrivalAirports { get; set; }
    public Airport()
    {
        DepartureAirports = new List&lt;FlightSchedule&gt;();
        ArrivalAirports = new List&lt;FlightSchedule&gt;();
    }
}

The Flight Schedule table:

[Table(&quot;FlightSchedule&quot;)]
public class FlightSchedule
{
    [Key]
    public Guid Id { get; set; }
    [Required]
    public Guid DepartureAirportId { get; set; }
    public Airport AirportDepart { get; set; }
    [Required]
    public Guid ArrivalAirportId { get; set; }
    public Airport AirportArrival { get; set; }
    [Required]
    public DateTime DepartureTime { get; set; }
    [Required]
    public DateTime ArrivalTime { get; set; }
    public ICollection&lt;Flight&gt; Flights { get; set; }
    public FlightSchedule()
    {
        Flights = new List&lt;Flight&gt;();
    }
}

And here is my config for DbContext in OnModelCreating() method:

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity&lt;Airport&gt;(entity =&gt;
        {
            entity.Property(e =&gt; e.Id).ValueGeneratedOnAdd();
        });

        modelBuilder.Entity&lt;FlightSchedule&gt;(entity =&gt;
        {
            entity.Property(e =&gt; e.Id).ValueGeneratedOnAdd();

            entity.HasOne(e =&gt; e.AirportDepart)
            .WithMany(e =&gt; e.DepartureAirports)
            .HasForeignKey(e =&gt; e.DepartureAirportId)
            .OnDelete(DeleteBehavior.ClientSetNull);

            entity.HasOne(e =&gt; e.AirportArrival)
                .WithMany(e =&gt; e.ArrivalAirports)
                .HasForeignKey(e =&gt; e.ArrivalAirportId)
              .OnDelete(DeleteBehavior.ClientSetNull);
        });

The diagram built from SQL server looks like what I designed but when I add some data seeder for those 2 tables, only Airport works, Flight Schedule would produce this error

> No relationship from 'Airport' to 'FlightSchedule' has been configured by convention because there are multiple properties on one entity type - {'ArrivalAirports', 'DepartureAirports'} that could be matched with the properties on the other entity types - {'AirportArrival', 'AirportDepart'}. This message can be disregarded if explicit configuration has been specified in 'OnModelCreating'.

I suspect maybe my configuration is wrong but I'm not sure where the cause is, since this is my first time trying with code first.

答案1

得分: 0

在您的表FlightSchedule中,DepartureAirportIdArrivalAirportId列需要设置为可空

FlightSchedule表中的记录只能有这些值中的一个,而不是两者同时拥有。

英文:

In your table FlightSchedule, columns DepartureAirportId and ArrivalAirportId need to be set up at nullable.

A record in FlightSchedule table can have only one of these values, not both.

huangapple
  • 本文由 发表于 2023年3月1日 16:19:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75601099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定