I'm using Entity Framework 6 to access a database. There are several slightly different versions of this database's schema that have been deployed over time. How can I access the database using EF when parts of the schema might be "missing", so to speak.
For example, a Tank
table was added to the database at some point, so I'd like to be able to add this to my context:
public DbSet<Tank> Tanks { get; set; }
and then perform queries against it, like so:
var tanks = context.Tanks.Where( ... ).ToList()
but obviously this throws an exception when running against versions of the schema that don't have this table.
Is there any way of dealing with this cleanly, other than catching the exception and checking for the appropriate SQL error code? Preferably some way of getting EF to check whether the table exists before I run the query? I couldn't see anything on the DbContext
class that would help, and an internet search didn't really turn up anything either. I suppose I could drop down to raw SQL to do it.
I should probably point out that there is no "schema version number" or similar column on any of the tables that would help to determine which version of the database is being accessed. Maybe this would have been a good idea (hindsight and all that) but it never happened, and retrofitting something like this to existing installations would be difficult.
Also, in my particular scenario, I am only reading data from an existing database: I do not need to write to the database using EF, nor do I need to get EF to build the schema for me.
EDIT: I just found this SO question: Entity Framework - How to check if table exists?, but the answer basically recommended performing a SQL query. That was from 2011 though, I'm wondering if EF has introduced anything cleaner in later versions.