Abstract your entites through an 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 Property
List<IGames> games;
And then load the entities based on their type (you can use switch
instead of the dictionary if you want. Question about 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 must add specific behavior for each specific object and expose it through the IGames
interface.
edit If you use the first approach of the model and generate entities, you can use partial classes to determine the implementation of the interface in separate files. EF already inserts the partial
keyword into the generated classes. For example, you can define:
public partial class MLB_Games : IGames {
Now the C # compiler will combine this definition and the generated definition of EF. This file will not be affected if you update your objects from the edmx model.
source share