如何检查表是否存在于模型中但不存在于数据库中
我有一个从数据库中提取的模型。后来,我从数据库中删除了该表,但没有更新模型。现在,我应该执行一个测试来测试该表是否存在于模型中但不存在于数据库中。谁能告诉我如何使用代码检查这一点?
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 using code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以连接到数据库并获取 information_schema.Tables 表,然后检查您期望拥有的所有表是否都在其中。
要获取 Linq-to-Sql DataContext 使用的所有表:
然后使用您选择的 sql 方法,在数据库上运行“SELECT [TABLE_NAME] FROM [Information_Schema].[Tables]”,并将其与你的数据上下文。
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:
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.
那么,一种快速而简单的方法可能是尝试使用模型和 Linq-to-Sql 在表中插入或选择某些内容,并检查是否抛出 SQLException。我不确定您是否可以捕获异常中的任何特定内容(例如 SQLTableDoesNotExistException),但是在调试器中应该很容易看到抛出的异常是否有任何特定属性,您可以区分其他 SQLException。
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.