我有两张表应该是这样的
flights
:flight_id,route_id,... routes
:route_id,... 我是Entity Framework的新手,无法弄清楚如何编写模型类,我尝试过这种方式:
public class Flight
{
[Key]
public int flight_id { get; set; }
[ForeignKey("route_id")]
public int route_id { get; set; }
public int airline_id { get; set; }
public DateTime departure_time { get; set; }
public DateTime arrival_time { get; set; }
}
public class Route
{
[Key]
public int route_id { get; set; }
public int origin_city_id { get; set; }
public int destination_city_id { get; set; }
public DateTime departure_date { get; set; }
}
我希望得到结果
ALTER TABLE [dbo].[Flights]
ADD CONSTRAINT [FK_dbo.Flights_dbo.route_id]
FOREIGN KEY ([route_id]) REFERENCES [dbo].[Routes] ([route_id])
ON DELETE CASCADE;
答案 0 :(得分:0)
您需要包含模型Route
,以便将其引用到您的模型Flight
。我可以看到你正在做一个1:n的关系,你可以这样做:
public class Flight
{
[Key]
public int flight_id { get; set; }
[ForeignKey("route_id")]
public int route_id { get; set; }
public int airline_id { get; set; }
public DateTime departure_time { get; set; }
public DateTime arrival_time { get; set; }
public List<Route> Routes {get; set} //including the Route Model for
//you to properly reference it.
}
如果您想要1:1关系,可以删除List
,以便它看起来像这样:
public Route Routing {get; set;}