我创建了两个代表数据库实体的类。
第一个实体:
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; }
}
当我尝试使用此代码时:
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);
}
我收到此错误
不支持每种类型的多个对象集。对象设置&#39; Flights&#39;和&#39; FlightsArchive&#39;都可以包含&#39; Project_Airline_Info.Models.Flight&#39;类型的实例。
所以我的问题是如何在DbContext类中创建具有相同泛型类型的2个DBset。
答案 0 :(得分:8)
简短的回答是你无法做到这一点。考虑这行代码:
var flight = context.Set<Flight>().Where(f => f.FlightNumber == "123");
它如何知道用于获取数据的集合?
最简单的解决方法可能是继承Flight
类并将其用于其他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; }
}
这样做的好处是,您现在可以为归档的航班添加属性,例如归档的日期:
public class ArchiveFlight : Flight
{
public DateTime DateArchived { get; set; }
}
答案 1 :(得分:1)
所以我的问题是如何在
DbSet
类中创建2个具有相同泛型的DbContext
。
您必须使用未用作DbSet
的基类,并继承您将用作DbSet
的类中的基类。
如果您使用继承的类-例如在以下示例中,FlightBase
-在DbSet
中,不会生成Flight
和ArchiveFlight
的表。
课程
public class FlightBase
{
// code
}
public class Flight : FlightBase { }
public class ArchiveFlight : FlightBase { }
DbContext
public class FlightsDatabase : DbContext
{
public DbSet<Flight> Flights { get; set; }
public DbSet<ArchiveFlight> ArchiveFlights { get; set; }
public DbSet<Passanger> Passengers { get; set; }
}
答案 2 :(得分:0)
您可以拥有2个dbset和2个数据库,如下所示:
private EntityContainer Context = new EntityContainer() { };
private EntityContainer ContextArchive = new EntityContainer("ArchiveEntityContainer") { };
//add this second constructor in your Context.tt file
public <#=code.Escape(container)#>(string connectionString) : base(connectionString) { };
//to have it in your autogenerated Context.cs file
public EntityContainer(string connectionString) : base(connectionString) { };
当然,您可以使用 using 语句。您必须将对象与 Context 分离,然后才能将其插入 ContextArchive 并相互