Abstract your entites via interface, for example:
class MLB_Games : IGames
{
public string Name { get; set; }
}
class NCAAF_Games : IGames
{
public string Name { get; set; }
}
class NFL_Games : IGames
{
public string Name { get; set; }
}
interface IGames
{
string Name { get; set; }
}
Create a property
List<IGames> games;
And then load entities based on their type (you can use switch
instead of dictionary, if you like. Matter of preference):
var gamesLoaders = new Dictionary<string, Func<IQueryable<IGames>>>
{
{"MLB", () => dbContext.MLB_Games},
{"NCAAF", () => dbContext.NCAAF_Games},
{"NFL", () => dbContext.NFL_Games}
};
var sport = "MLB";
if(gamesLoaders.ContainsKey(sport))
games = gamesLoaders[sport]().ToList();
else
Logger.Instance.LogMessage("Cannot initialize the requested sport: " + sport);
Now you can work with one list of games
. Note, that you should add specific behaviour for each concrete entity and expose it via IGames
interface.
edit:
If you are using model first approach and generate entities, you can use partial classes to define interface implementation in separate files. EF already insert partial
keyword to generated classes. For example you can define:
public partial class MLB_Games : IGames
{
//interface implementation is defined in generated file
}
Now C# compiler will combine this definition and EF generated definition. This file won't be affected when you will update your entities from edmx model.