简体   繁体   English

从EntityFramework的`DbContext`中获取数据库类型

[英]Get the database type from EntityFramework's `DbContext`

The question is really simple: given an instance of an EntityFramework's DbContext , I would like to tell between the effective database it is connected do. 问题很简单:给定一个EntityFramework的DbContext实例,我想在它所连接的有效数据库之间做什么。 AFAIK there are current implementations of EF providers for SQL Server, by Microsoft, MySQL, from Oracle, and perhaps for Postgres. AFAIK目前有针对SQL Server的EF提供程序的实现,由Microsoft,MySQL,Oracle和Postgres提供。

Let's assume I have access "only" to the DbContext instance in my method. 假设我在我的方法中只“访问”了DbContext实例。 How do I detect that I am working with MSSQL, MySQL, Postgres, etc.? 我如何检测到我正在使用MSSQL,MySQL,Postgres等? (I wonder if there is an Oracle client for EF, that would add up) (我想知道是否有一个用于EF的Oracle客户端,这会加起来)

Currently I can try with some reflection: 目前我可以尝试一些反思:

  • DbContext.Connection is normally an instance of EntityConnection DbContext.Connection通常是EntityConnection一个实例
  • EntityConnection exposes StoreConnection property, which should be our ADO connection. EntityConnection公开StoreConnection属性,该属性应该是我们的ADO连接。 testing it against known classes (like MySqlConnection ) should work 针对已知类(如MySqlConnection )进行测试应该可行

However I believe that this approach is a little tricky. 但是我相信这种方法有点棘手。 A normal App.config contains the following: 普通的App.config包含以下内容:

<providers>
  <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>

I am a little confused, I hoped EF had a better API to detect database type in case one needs to perform uncommon operations. 我有点困惑,我希望EF有一个更好的API来检测数据库类型,以防需要执行不常见的操作。

My question is if there is a simpler method. 我的问题是,是否有一个更简单的方法。

Did you use this ? 你用过这个吗?

DbContext.Database.Connection.GetType().Name

If yes, what is the tricky part ? 如果是,那么棘手的部分是什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您如何使用Moles来阻止EntityFramework 4.1中的数据库查询DbContext? - How do you use Moles to mole DbContext from querying the database in EntityFramework 4.1? 表类从EntityFramework的DBcontext对象中消失 - Table Class disappears from DBcontext object of EntityFramework 您可以从 EntityFramework Core 中的另一个 DbContext 创建一个子 DbContext 吗? - Can you create a sub DbContext from another DbContext in EntityFramework Core? EntityFramework Core 无法更新数据库“id's not same type” - EntityFramework Core can't update Database “id's not same type” 如何从 Entity Framework Core 中的 DbContext 获取列名和相应的数据库类型 - How to get Column name and corresponding Database Type from DbContext in Entity Framework Core 带有EntityFramework的WCF服务,给出错误“在未引用的程序集中定义了&#39;DBContext&#39;类型” - WCF Service with EntityFramework, giving error “The type 'DBContext' is defined in an assembly that is not referenced” 使用EntityFramework dbContext在另一个表中获取带有count的记录 - Using EntityFramework dbContext to get records with count in another table 获取EntityFramework的拦截中的实体类型 - Get Entity Type in Interception for EntityFramework 使用ASP.NET MVC和EntityFramework从DbContext中排除列 - Exclude a column from DbContext using ASP.NET MVC and EntityFramework 如何清空EntityFramework DbContext? - How to empty an EntityFramework DbContext?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM