Get database type from EntityFramework `DbContext`

The question is very simple: this instance of EntityFramework DbContext , I would like to say between the efficient database with which it is connected. AFAIK, there are current implementations of EF providers for SQL Server, Microsoft, MySQL, Oracle, and possibly for Postgres.

Suppose I only have access to the DbContext instance in my method. How to determine that I am working with MSSQL, MySQL, Postgres, etc.? (I wonder if there is an Oracle client for EF that will add up)

Currently I can try with some reflection:

  • DbContext.Connection usually an instance of EntityConnection
  • EntityConnection provides the EntityConnection property, which should be our ADO connection. testing it against well-known classes (e.g. MySqlConnection ) should work

However, I find this approach a bit complicated. The regular App.config file contains the following:

 <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'm a little confused, I was hoping that EF had a better API for detecting the type of database if you need to perform unusual operations.

My question is is there a simpler method.

+5
source share
1 answer

Did you use this?

 DbContext.Database.Connection.GetType().Name 

If so, what is the hard part?

+2
source

All Articles