5

我正在使用 EF 4.3.1 CodeFirst 进行开发。我有一个机场表,如下所示:

 public class Airport
    {
        [Key]
        public int ID { get; set; }
        public string Name{ get; set; }
    }

我需要的是一个 Route 表,其中包含来自同一个 Airport 表的 2 个 FK,例如:

 public class Route
    {
        public int DepartureAirportID { get; set; }
        public int DestinationAirportID { get; set; }
        public virtual Airport DestinationAirport { get; set; }
        public virtual Airport DepartureAirport { get; set; }
    }

如何做到这一点?

4

2 回答 2

4

这应该做你需要的......

public class Airport
{
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Route> DepartureRoutes { get; set; }
    public virtual ICollection<Route> DestinationRoutes { get; set; }
}
public class Route
{
    public int DepartureAirportID { get; set; }
    public int DestinationAirportID { get; set; }
    public Airport DestinationAirport { get; set; }
    public Airport DepartureAirport { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Route>()
        .HasKey(i => new { i.DepartureAirportID, i.DestinationAirportID});

    modelBuilder.Entity<Route>()
        .HasRequired(i => i.DepartureAirport)
        .WithMany(u => u.DepartureRoutes)
        .HasForeignKey(i => i.DepartureAirportID)
        .WillCascadeOnDelete(false);

    modelBuilder.Entity<Route>()
        .HasRequired(i => i.DestinationAirport)
        .WithMany(u => u.DestinationRoutes)
        .HasForeignKey(i => i.DestinationAirportID)
        .WillCascadeOnDelete(false);
}

...这会创建像...这样的表格

CREATE TABLE [Airports] (
    [ID] [int] NOT NULL IDENTITY,
    [Name] [nvarchar](4000),
    CONSTRAINT [PK_Airports] PRIMARY KEY ([ID])
)
CREATE TABLE [Routes] (
    [DepartureAirportID] [int] NOT NULL,
    [DestinationAirportID] [int] NOT NULL,
    CONSTRAINT [PK_Routes] PRIMARY KEY ([DepartureAirportID], [DestinationAirportID])
)
CREATE INDEX [IX_DestinationAirportID] ON [Routes]([DestinationAirportID])
CREATE INDEX [IX_DepartureAirportID] ON [Routes]([DepartureAirportID])
ALTER TABLE [Routes] ADD CONSTRAINT [FK_Routes_Airports_DestinationAirportID] FOREIGN KEY ([DestinationAirportID]) REFERENCES [Airports] ([ID])
ALTER TABLE [Routes] ADD CONSTRAINT [FK_Routes_Airports_DepartureAirportID] FOREIGN KEY ([DepartureAirportID]) REFERENCES [Airports] ([ID])

......你可以像这样使用它......

using (var db = new MyDbContext())
{
    foreach (var routeid in Enumerable.Range(1, 100))
    {
        var departure = new Airport { Name = "departure" + routeid };
        db.Airports.Add(departure);
        var destination = new Airport { Name = "destination" + routeid };
        db.Airports.Add(destination);

        var route = new Route{ DepartureAirport = departure, DestinationAirport = destination };
        db.Routes.Add(route);
    }

    int recordsAffected = db.SaveChanges();

    foreach (var route in db.Routes)
    {
        Console.WriteLine("{0}, {1}, {2}, {3}", route.DepartureAirportID, route.DestinationAirportID, route.DepartureAirport.Name, route.DestinationAirport.Name);
    }
}

...希望这可以帮助。注意:不要virtual在必需的属性上使用(因为这些是索引 - 对于这种类型的映射只会像那样工作,我认为你会得到一些错误)。
此外,我总是添加相反的关系,但您可以使用 WithMany() 空白,应该也可以。

于 2012-04-06T20:39:49.280 回答
0

您应该在您的类 Airport 中添加一个额外的自相关属性:

public class Airport
{
    public int ID { get; set; }
    public string Name{ get; set; }
    public virtual ICollection<Airport> AirportRoutes { get; set; }
}

并使用下一个 Fluent API 代码覆盖 OnModelCreating 方法:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<Airport>().HasMany(x => x.AirportRoutes).WithMany().Map(
            x => 
               {
                   x.MapLeftKey("DepartureId");       // left key name
                   x.MapRightKey("DestinationId");    // right key name
                   x.ToTable("Routes");               // table name
               });
}

希望这对您有所帮助。

于 2012-04-18T08:25:44.493 回答