简体   繁体   English

实体框架如何使用代码优先方法生成此SQL代码?

[英]Entity Framework how to produce this SQL code with code first approach?

I have 2 tables that should look like this 我有2张桌子应该看起来像这样

  • flights : flight_id, route_id,... flights :flight_id,route_id,...
  • routes : route_id,... routes :route_id,...

I am new to Entity Framework and can't figure out how to write model classes so well, I have tried this way: 我是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; }
}

and I would like to get as a result 我想得到的结果

ALTER TABLE [dbo].[Flights]
    ADD CONSTRAINT [FK_dbo.Flights_dbo.route_id] 
        FOREIGN KEY ([route_id]) REFERENCES [dbo].[Routes] ([route_id]) 
        ON DELETE CASCADE;

You need to include the model Route in order for you reference it to your model Flight . 您需要包括Route模型,以便将其引用到Flight模型中。 As I can see that you are doing a 1:n relationship, you can do it like this: 正如我所看到的,您正在做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.
}

You can remove the List if you want a 1:1 relationship so that it would look like this: 如果您想要1:1关系,可以删除List ,这样它看起来像这样:

public Route Routing {get; set;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架 - 代码优先方法 - Entity Framework - Code First Approach 实体框架代码优先方法 - Entity Framework Code First Approach 如何使用实体框架 6 代码优先方法在 SQL Server 中创建序列并应用两个表? - How to create the sequence and apply two tables in SQL Server using Entity framework 6 code first approach? 如何通过实体框架代码优先方法定义导航属性 - How to define a navigation property via Entity Framework code first approach 如何使用实体框架代码优先方法正确创建数据库 - How to correctly create a database using entity framework code first approach 在实体框架代码第一种方法中映射字典 - Map a Dictionary in Entity Framework Code First Approach 如何在Entity Framework 6中禁用模型缓存(Code First方法) - How to disable model caching in Entity Framework 6 (Code First approach) 如何使用Entity Framework 7在代码中首先处理这两个模型? - How handle these two models in code first approach using Entity Framework 7? 外键与实体框架代码优先方法 - Foreign key with entity framework code first approach 如果我们使用具有代码优先方法的Entity框架,是否可以在给定路径上创建数据库(Sql Server compact)? - Is possible to create a database (Sql Server compact) on a given path if we use Entity framework with code-first approach?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM