实体框架 - 如何检查表是否存在?
我使用实体框架和代码优先方法。基类 DbContext 具有创建和删除数据库以及检查数据库是否存在的函数。
我想检查特殊表(实体)是否存在。是否可以使用框架实现或者我需要编写自定义方法?如果我需要编写自己的实现,那么最通用的方法是什么?
感谢您的任何帮助。
I'm using the Entity Framework with Code First approach. The base class DbContext has functions to create and delete the database as well as to check for its existence.
I want to check if a special table (entity) is existing or not. Is it possible with an framework implementation or do I need to write custom methods? If I need to write my own implementation, what would be the most generic approach to do that?
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您需要检查表是否存在,则必须调用自定义 SQL 代码:
表名称默认定义为派生上下文中公开的
DbSet
名称,但默认名称可以通过 Fluent API 覆盖ToTable
方法或Table
数据注释。以通用方式执行此操作并不是代码优先方法中所设想的。这将需要浏览元数据并手动探索实体映射到哪个表 - 这可能非常复杂,因为实体可以映射到多个表。代码优先不提供对元数据的访问。您必须将
DbContext
转换为ObjectContext
并浏览MetadataWorkspace
。编辑:
要将
DbContext
转换为ObjectContext
使用以下命令:If you need to check existence of the table you must call custom SQL code:
Table name is defined by default as the name of
DbSet
exposed on your derived context but the default name can be overriden either by fluent API'sToTable
method orTable
data annotation.Doing this in the generic way is not something supposed in code first approach. That will require browsing metadata and manually explore to which table is the entity mapped - this can be pretty complex because entity can be mapped to multiple tables. Code first doesn't offer access to metadata. You must convert
DbContext
toObjectContext
and browseMetadataWorkspace
.Edit:
To convert
DbContext
toObjectContext
use this:我无法对上一篇文章添加评论。我正在使用 SQL Compact,但我不知道表的架构。我正在使用此代码来检查表,它与上一篇文章中的代码非常相同,但它适用于任何表。
I can't add comment to previous post. I'm using SQL Compact and I don't know schema of the table. I'm using this code to check for table It's pretty the same that in previous post but It works for any table.
另一种方法;它不像 Ladislav 那样高效,但它不依赖于 SQL Server(经过编辑以添加Where子句来解决性能问题):
An alternative method; it's not as efficient as Ladislav's, but it's not tied to SQL Server (edited to add Where clause to address performance issue):
在 EF Core 中,我添加了 TableExists 方法作为 DbContext 类的扩展方法。这是我使用 Dapper 的解决方案。
这是一个用法示例:
希望这对其他人有帮助。
In EF Core, I have added the TableExists method as a extension method for the DbContext class. Here is my solution using Dapper.
And here is an usage example:
Hope this help other people.
假设:SQL Server
在查询
DbSet
时捕获任何旧异常并不意味着该表不存在。查询表不存在的
DbSet
将引发EntityCommandExecutionException
以及类型为SqlException
的内部异常。该内部异常有一个ErrorNumber
属性。错误号 208 读取(来源):
Assumption: SQL Server
Catching any old exception when querying the
DbSet
does not mean the table does not exist.Querying a
DbSet
where the table does not exist will throw anEntityCommandExecutionException
with an inner exception of typeSqlException
. That inner exception has anErrorNumber
property.Error number 208 reads (source):
我认为下面的代码更容易理解。
I think following code is a little bit more understandable.
以下是在 EF Core 中执行此操作的扩展方法(无需 Dapper):
Here's an extension method for doing this in EF Core (without Dapper):
如果不存在,此代码自动创建所有表
this code create all tables automatic if dose not exist