如何检查表是否存在于模型中但不存在于数据库中

发布于 2024-10-08 16:41:32 字数 97 浏览 1 评论 0原文

我有一个从数据库中提取的模型。后来,我从数据库中删除了该表,但没有更新模型。现在,我应该执行一个测试来测试该表是否存在于模型中但不存在于数据库中。谁能告诉我如何使用代码检查这一点?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

望她远 2024-10-15 16:41:32

您可以连接到数据库并获取 information_schema.Tables 表,然后检查您期望拥有的所有表是否都在其中。

要获取 Linq-to-Sql DataContext 使用的所有表:

var dataContext = new DataContext();
var dataContextTableNames = (from tables in dataContext.Mapping.GetTables()
                             select tables.TableName).ToList();

然后使用您选择的 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:

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.

つ低調成傷 2024-10-15 16:41:32

那么,一种快速而简单的方法可能是尝试使用模型和 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.

为你拒绝所有暧昧 2024-10-15 16:41:32

嗯..看来您正在尝试编写新的测试。在删除这些表之前,您是否没有首先进行集成测试来验证表和实体的交互?

一旦从数据库中删除表,这些集成测试就会失败。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文