Configuring 3rd party Entity Framework provider (Firebird) from code
When you’re using Entity Framework 6 with some 3rd party provider – i.e. EntityFramework.Firebird or EntityFramework.NuoDb (disclaimer: I’m author of both) – you need to give Entity Framework few hints where to find some methods and classes. Maybe you’ve seen classes like DbProviderFactory
or DbProviderServices
. Often when you install the provider, your app.config
is updated accordingly. But you might want to do it in code. Either because you want to to have it strongly typed or because you don’t want to think about what project, that’s using the DAL project, is going to be executed and so on.
Unsurprisingly the app.config
is not the only option where you can do such configuration. You can create a class that inherits from DbConfiguration
and do the same there. And not only this, although I’ll focus only on task currently in hand.
The classes mentioned above were not random choice. The fact is these two you need to configure (if you provide DbConnection
to your DbContext
). Thus for, for example, Firebird you can put these two lines into the constructor.
SetProviderServices(FirebirdSql.Data.EntityFramework6.FbProviderServices.ProviderInvariantName, FirebirdSql.Data.EntityFramework6.FbProviderServices.Instance);
SetProviderFactory(FirebirdSql.Data.EntityFramework6.FbProviderServices.ProviderInvariantName, FirebirdSql.Data.FirebirdClient.FirebirdClientFactory.Instance);
The ProviderInvariantName
field is a constant "FirebirdSql.Data.FirebirdClient"
, so you don’t have to type it yourself and be sure the classes are matching the provider.
Finally put the DbConfigurationType
attribute on your context and specify the name of DbConfiguration
class you created above.
And that’s it. Just a few lines of code and the DAL project is now self contained without the need to have some external configuration.