如何首先在Entity Framework代码中创建2个具有相同类型的dbset? [英] How to create 2 dbsets with the same type in Entity Framework code first?

查看:52
本文介绍了如何首先在Entity Framework代码中创建2个具有相同类型的dbset?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了2个代表数据库实体的类。

I created 2 classes which represent entities for a database.

第一个实体:

public enum FlightStatus
{
        checkIn,
        gateClosed,
        arrived,
        departedAt,
        unknown,
        canceled,
        expectedAt,
        delayed,
        InFlight
}

public class Flight 
{
        [Key]
        public string FlightNumber { get; set; }
        public DateTime Arrival { get; set; }
        public DateTime Departure { get; set; }
        public string CityOfArrival { get; set; }
        public string CityOfDeparture { get; set; }
        public char Terminal { get; set; }

        public FlightStatus Status { get; set; }
        public int Gate { get; set; }

        public virtual ICollection<Passanger> PassengerList { get; set; }
        public double PriceForFirstClass { get; set; }
        public double PriceForBusiness { get; set; }
        public double PriceForEconom { get; set; }
}

第二实体:

public enum Sex
{
        M,
        F
}

public enum FlightClass
{
        First,
        Business,
        Econom,
}

public enum TicketType
{
        OneWay,
        TwoWay,
}

public class Passenger
{
        [Key]
        public string Passport { get; set; }
        public string flightNumber { get; set; }

        [ForeignKey("flightNumber")]
        public virtual Flight flight { get; set; }      

        public string FirstName { get; set; }
        public string SecondName { get; set; }
        public string Nationality { get; set; }       
        public DateTime DateOfbirthday { get; set; }
        public Sex SexOfPassanger { get; set; }
        public FlightClass ClassForPassanger { get; set; }
        public TicketType TypeOfTicket { get; set; }
}

然后我创建了 DbContext

class FlightsDatabase : DbContext
{
    public DbSet<Flight> Flights { get; set; }
    public DbSet<Flight> FlightsArchive { get; set; }

    public DbSet<Passenger> Passengers { get; set; }
}

当我尝试使用此代码时:

When I try to use this code:

using (FlightsDatabase flights = new FlightsDatabase())
{
    foreach (Flight flight in flights.Flights)
    {
        if (Math.Abs((flight.Departure - DateTime.Now).Days) <= 1)
        {
            timeCame(flight, null);
        }

        if (flight.Departure < DateTime.Now)
        {
            flightsToClean.Add(flight);
        }
    }

    moveOldFlights(flightsToClean, null);
}

我收到此错误


不支持每种类型的多个对象集。对象集 Flights和 FlightsArchive都可以包含类型为 Project_Airline_Info.Models.Flight的实例。

Multiple object sets per type are not supported. The object sets 'Flights' and 'FlightsArchive' can both contain instances of type 'Project_Airline_Info.Models.Flight'.

所以我的问题如何在DbContext类中创建具有相同泛型的2个DBset。

So my question is how to create 2 DBsets with same generic type,in DbContext class.

推荐答案

简短的答案是您不能这样做。考虑以下代码行:

The short answer is that you can't do this. Consider this line of code:

var flight = context.Set<Flight>().Where(f => f.FlightNumber == "123");

如何知道要使用哪个集合来获取数据?

How does it know which set to use to get the data?

可能最简单的解决方法是继承 Flight 类,并将其用于其他 DbSet

Probably the simplest workaround would be to inherit the Flight class and use that for your other DbSet:

public class ArchiveFlight : Flight
{
}

以及您的环境:

public class FlightsDatabase :DbContext
{
    public DbSet<Flight> Flights { get; set; }
    public DbSet<ArchiveFlight> FlightsArchive { get; set; }
    public DbSet<Passanger> Passengers { get; set; }
}

这样做的好处是您现在可以将属性添加到已归档文件中航班,例如存档日期:

The bonus of doing this is that you can now add properties to your archived flights, such as the date it was archived:

public class ArchiveFlight : Flight
{
    public DateTime DateArchived { get; set; }
}

这篇关于如何首先在Entity Framework代码中创建2个具有相同类型的dbset?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆