Reputation: 26874
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. AFAIK there are current implementations of EF providers for SQL Server, by Microsoft, MySQL, from Oracle, and perhaps for Postgres.
Let's assume I have access "only" to the DbContext
instance in my method. How do I detect that I am working with MSSQL, MySQL, Postgres, etc.? (I wonder if there is an Oracle client for EF, that would add up)
Currently I can try with some reflection:
DbContext.Connection
is normally an instance of EntityConnection
EntityConnection
exposes StoreConnection
property, which should be our ADO connection. testing it against known classes (like MySqlConnection
) should workHowever I believe that this approach is a little tricky. A normal App.config 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 am a little confused, I hoped EF had a better API to detect database type in case one needs to perform uncommon operations.
My question is if there is a simpler method.
Upvotes: 8
Views: 3367
Reputation: 18127
Did you use this ?
DbContext.Database.Connection.GetType().Name
If yes, what is the tricky part ?
Upvotes: 6