c# - Get the database type from EntityFramework's `DbContext` -


the question simple: given instance of entityframework's dbcontext, tell between effective database connected do. afaik there current implementations of ef providers sql server, microsoft, mysql, oracle, , perhaps postgres.

let's assume have access "only" dbcontext instance in method. how detect working mssql, mysql, postgres, etc.? (i wonder if there oracle client ef, add up)

currently can try reflection:

  • dbcontext.connection instance of entityconnection
  • entityconnection exposes storeconnection property, should our ado connection. testing against known classes (like mysqlconnection) should work

however believe approach little tricky. normal app.config contains 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 little confused, hoped ef had better api detect database type in case 1 needs perform uncommon operations.

my question if there simpler method.

did use ?

dbcontext.database.connection.gettype().name 

if yes, tricky part ?


Comments