英文:
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
public ICollection
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
public FlightSchedule()
{
Flights = a new List
}
}
这是我的DbContext配置在OnModelCreating()方法中:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.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("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<FlightSchedule> DepartureAirports { get; set; }
public ICollection<FlightSchedule> ArrivalAirports { get; set; }
public Airport()
{
DepartureAirports = new List<FlightSchedule>();
ArrivalAirports = new List<FlightSchedule>();
}
}
The Flight Schedule table:
[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<Flight> Flights { get; set; }
public FlightSchedule()
{
Flights = new List<Flight>();
}
}
And here is my config for DbContext in OnModelCreating() method:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Airport>(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);
});
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
中,DepartureAirportId
和ArrivalAirportId
列需要设置为可空。
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论