Thursday 13 November 2014

How to use Entity Framework with Oracle and ODP.NET in Visual Studio 2013 MVC 5.0

How to use Entity Framework with Oracle and ODP.NET in Visual Studio 2013 MVC 5.0
This step-by-step walkthrough provide an introduction to Oracle database development using Entity Framework. This example shows you how to reverse engineer a model from an existing database. The model is stored in an EDMX file  and can be viewed and edited in the Entity Framework Designer. The classes that you interact with in your application are automatically generated from the EDMX file.
1. Download Oracle Developer Tools for Visual Studio: To use Visual Studio's Entity Designer for Database First and Model First object-relational and mapping, Data sources Window, the Dataset Designer, and the Table Adapter Configuration Wizard to drag and drop and automatically generate .NET code, you have to install Oracle developer tools for visual studio.
http://www.oracle.com/technetwork/developer-tools/visual-studio/overview/index.html

2. Create your application: Click New Project, then select Visual C# on the left, then Web and then select ASP.NET  Web Application. Name your project "MvcTest" and then click OK. In the New ASP.NET Project dialog, click MVC and then click OK.
3. The default template gives you  Home, Contact and About, Register and Login pages. You do not want to have register and login pages because they use the newer version (6.0) of entity framework.  Entity Framework 6 is not supported at this time with Oracle 12c or any version of Oracle before 12c. Remove Account folder, Account controller and anything related to account, register, login, and partial login from View, Controller and Model folders from Solution explorer.

4. Nuget Package Manager updade: Before you get too excited to upgrade your entity framework from 6.0 to 6.x.x in Nuget Package Manager, stop right there. Visual Studio 2013 defaults to Entity Framework 6. You will have to set your .NET project to use an earlier version of Entity Framework like 5.0. That means you have to uninstall your entity framework 6.x.x first. As a matter of fact, you will have to uninstall all of its dependencies such as Microsoft ASP.NET identity framework as well. (note to myself: DLL hell era is not over. Microsoft just replaced it with NuGet packages. You can not have two different versions of entity frameworks in the same application domain).
Go to tools -> library package manager -> manage nuget solution for package

  
After the entity framework 6.0 and all its dependencies are uninstalled. you will need to install entity framework 5.0: Install-Package EntityFramework -Version 5.0.0




5. Add a data connection and set the Filters. If you are the owner of the schema, you won't be able to see the other schemas tables or views unless you add the required schemas to your filter. To do that, Open up the server explorer, add/select your database connection, right click on your connection, click on filters.(Note to my self: You can do the same thing when you create ADO.NET Entity Data Model)


6. Clean up the providers that you don't need. You could simply remove "providers" from your entityframwork tags in the web.config. If you don't, you might get an error like unable to retrieve metadata for 'path' unrecognized element providers. (C:\Users\user\appdata\local\Temp-mp6124.tmp line 78)
  <providers>      <provider invariantName="System.Data.SqlClienttype="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />    </providers> The entityFramework tag should look like:<entityFramework><defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"><parameters><parameter value="v12.0" /></parameters></defaultConnectionFactory>   </entityFramework>
8. Install Oracle Data Provider for .NET (ODP.NET) Managed Driver. Everything might work perfectly on your local machine if you have the provider installed. But when you deploy your web site, you get this error: "Unable to find the requested .Net Framework Data Provider. It may not be installed." or "Failed to find or load the registered .Net Framework Data Provider"
There are several ways to install Oracle Data Provider for .NET (ODP.NET) Managed Driver. If you like NuGet , run the following command in the Package Manager Console:
PM> Install-Package odp.net.managed -Version 121.1.0
So when you deploy your code, your managed driver will be included in your deployment package and will be available in the remote machine. If you just add Oracle.ManaedDataAccess.dll as a reference without installing the package, it won't work.
http://www.nuget.org/packages/odp.net.managed/
Oracle Data Provider for .NET (ODP.NET) features optimized ADO.NET data access to the Oracle database. ODP.NET allows developers to take advantage of advanced Oracle database functionality, including Real Application Clusters, XML DB, and advanced security. The data provider can be used with the latest .NET Framework 4.5.1 version.

ODP.NET makes using Oracle from .NET more flexible, faster, and more stable. ODP.NET includes many features not available from other .NET drivers, including flexible LOB data types, self-tuning, run-time connection load balancing, fast connection failover, and Advanced Queuing.
9. Add an ADO.NET Entity Model to your project. choose model content as Generate from database




















10. Create your data connection and save the entity connection settings in Web.Config.






















11. Choose Entity Framework 5.0 and hit the  next button to include some database objects in your model.


























Once the reverse engineer process completes the new model is added to your project and opened up for you to view in the Entity Framework Designer. The classes we are going to use to access data are being automatically generated for you based on the EDMX file.  if you are using Visual Studio 2013 the Model.tt and Model.Context.tt files will be nested under the EDMX file. An App.config file has also been added to your project with the connection details for the database.































Note: if you update the schema in the database, you will have to update the model. To do that: 
  • Right-click on an empty spot of your model in the EF Designer and select ‘Update Model from Database…’, this will launch the Update Wizard
  • On the Add tab of the Update Wizard check the box next to Tables, this indicates that we want to add any new tables from the schema.

  • The Refresh tab shows any existing tables in the model that will be checked for changes during the update. The Delete tabs show any tables that have been removed from the schema and will also be removed from the model as part of the update. The information on these two tabs is automatically detected and is provided for informational purposes only, you cannot change any settings.






























12. Add a new MVC 5 controller:  In Solution  Explorer, right-click the Controllers folder  and then click Add,  then ControllerIn the Add Scaffold dialog box, click MVC 5  Controller with view, using Entity Framework, and then click Add.
 









        4 comments:

        1. Very good post.. Really helped in getting started with ODP.NET

          ReplyDelete
        2. is this now outdated i.e. does oracle managed driver now support EF6 ?

          ReplyDelete
          Replies
          1. Andy, I read that if you have ODAC 12c R2 installed, it doesn't support EF6. You need to install ODAC 12c R3 instead. The ODT version is 12.1.0.2 in the R3 version.

            Delete

        Angular Tutorial (Update to Angular 7)

        As Angular 7 has just been released a few days ago. This tutorial is updated to show you how to create an Angular 7 project and the new fe...