How to check if table exists in model but not in Database
I have a model extracted from database. Later, I have deleted the table from database but not updated the model. Now, I should perform a test to test if the table exists in Model but not in database. Can any one tell me how to check this开发者_JS百科 using code?
You could connect to the database and grab the information_schema.Tables table, then check to see if all the tables you expect to have are in there.
To get all the tables used by your Linq-to-Sql DataContext:
var dataContext = new DataContext();
var dataContextTableNames = (from tables in dataContext.Mapping.GetTables()
select tables.TableName).ToList();
Then using your sql method of choice, you'd run "SELECT [TABLE_NAME] FROM [Information_Schema].[Tables]" on your database and compare it to those in your DataContext.
Well, a quick and simple way may be to try to then Insert or Select something into or from the table using the Model and Linq-to-Sql and check if a SQLException is thrown. I'm not sure if you can catch anything specific in the exception like SQLTableDoesNotExistException, but it should be pretty easy to see in a debugger if there is any specific properties of the exception thrown that you can differentiate between other SQLExceptions.
Hmm.. it looks like you are trying to write new tests. Don't you have integration tests to verify the table and entity interactions in the first place before you remove these tables?
Those integrations tests would fail as soon as table is removed from database.
精彩评论