pátek 7. března 2014

Devart dotConnect for Oracle - Entity Framework provider


Recently we evaluated entity framework, we use oracle as a database on most of our projects. The default EF provider is for SqlServer. We have searched for an oracle provider. There is a provider directly from Oracle (ODP.NET provider), this has limited database support (oracle 9 is not supported anymore), and offers only database first and model first approach. Code first is not officially supported. An alternative to ODP.NET is a commercial provider from devart – devart dotconnect for Oracle. This has support for older databases and for code first and the latest EF (EF 6) too.

After you buy a licence you can install the provider to your development machine. All the devart assemblies are installed to GAC. The first thing I did was to copy the assemblies to a versioned local LIB folder and reference the assemblies from there. This way you know what you are referencing. GAC causes several problems in deployment, assemblies referenced from GAC are not deployed during build process, you need to manually set Copy Local to true on every assembly. You need to do it even if you reference it from a LIB, but you are on a machine (Development manchine)which has it in a GAC, it won’t be copied to output without setting copy local to true. Another thing are indirect references, a client application references an assembly which has reference to devart assemblies, they are not copied to output when deploying the client application even if you set copy local to true, you need to reference Devart directly from the client application and set copy local to true on every assembly you reference. This is not related to Devart, this works the same way on every assembly from GAC I think.

The key assemblies for entity framework support are:
Devart.Data
Devart.Data.Oracle
Devart.Data.Oracle.Entity


Next, you need to tell entity framework that there is a devart entity framework provider for oracle.

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="Devart.Data.Oracle" type="Devart.Data.Oracle.Entity.OracleEntityProviderServices,
      Devart.Data.Oracle.Entity, Version=8.2.90.6, Culture=neutral, PublicKeyToken=09af7300eec23701" />   
    </providers>
  </entityFramework>
<system.data>
  <DbProviderFactories>
    <remove invariant="Devart.Data.Oracle" />
    <add name="dotConnect for Oracle" invariant="Devart.Data.Oracle"
     description="Devart dotConnect for Oracle"
     type="Devart.Data.Oracle.OracleProviderFactory, Devart.Data.Oracle,
       Version=8.2.90.0, Culture=neutral, PublicKeyToken=09af7300eec23701" />
  </DbProviderFactories>
</system.data>

And finally the connection string for the db context is:
<connectionStrings>
<add name="OnlineDataDbContextConnectionString" connectionString="User Id=*;Password=*;Server=*;Home=oraclient11g_home1;Persist Security Info=True;" providerName="Devart.Data.Oracle"/>
</connectionStrings>


There is an interesting note about the connection string, the Home property. This property is not a standard property (at least not in System.Data.OracleClient which we used before Devart). Home property specifies which oracle home to use when connecting to database. You can have multiple versions of oracle client installed on your computer (with multiple oracle homes), Devart gives you a possibility to choose the right oracle home.  System.Data.OracleClient is an oracle driver written by Microsoft (and currently is deprecated), and it is not using this Home property. It simply uses the first oracle home which it finds on the computer (particularly it scans the PATH environment variable, and takes the first path to oracle home from there). Devart forces you to explicitly name the oracle home. This could be tricky if your oracle homes are named differently on the development machines,or build servers or production machines. Be sure that on every machine the same name for oracle home is used.


This makes devart entity framework oracle provider work on the machine where you installed the devart package (assemblies in GAC and licence information set). You need to do additional setup when deploying to a different machine (where you don’t want to install devart), referencing the devart assemblies from client and set their copy local attribute to true as we mentioned above and another extra step is to generate licence information to assemblies which are using devart. If you omit this, Devart will complain on that machine and say it is not licenced.


How devart licencing works in general is available here: http://www.devart.com/dotconnect/mysql/docs/Licensing.html
The easiest way to make licencing work is from Visual Studio, choose the licencnig menu item as shown on the picture below:




All the available assemblies are listed and an UI is provided to fix the problems – generate licencing information. Just follow the wizard and you assemblies will be able to use devart on different machine too.



In next blog post I’ll show you how to get rid of System.Data.OracleClient for classic AdoNET access and use devart instead.






Žádné komentáře:

Okomentovat