我正在尝试在表格中创建具有不同关系和复合键的4个表。
关系模式如下所示:
我已经准备好的课程:
public class Bus
{
[Key]
public int VIN { get; set; }
[Required]
public string RegNum { get; set; }
public string Status { get; set; }
public virtual ICollection<Line> Lines { get; set; }
}
public class Line
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Key]
[Column(Order = 0)]
public int LineID { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Key]
[Column(Order = 1)]
public string Direction { get; set; }
public virtual ICollection<Schedule> Schedules { get; set; }
public virtual ICollection<Bus> Buses { get; set; }
}
public class Stop
{
[Key]
public int StopID { get; set; }
public string Street { get; set; }
}
public class Schedule
{
[Key]
public int ScheduleID { get; set; }
[ForeignKey("Line")]
[Column(Order = 0)]
public int LineID { get; set; }
[ForeignKey("Line")]
[Column(Order = 1)]
public string Direction { get; set; }
[ForeignKey("Stop")]
public int StopID { get; set; }
public int BusOrder { get; set; }
public virtual Line Line { get; set; }
public virtual Stop Stop { get; set; }
}
如何为此方案构建适当的数据库上下文?
我已经为Schedule表创建了那些Data Annotation,但是不确定如何在Bus和Line之间进行多对多连接。我是否需要创建连接表 - 它是否会更改ERD? 我很感激你的帮助!;-) KB