abiNerd
abiNerd

Reputation: 2033

How to get the ASP.NET MVC 4 model to link to an Oracle database

This is my first MVC application so please be patient and kind. I have created an ASP.NET MVC 4 Web Application in Visual Studio 2012. I created the database code first, so all I did was create the model and a data context and a SQL server database was created for me. Every time I changed the model I could just update the database. I also connected to an Oracle database get other data that I needed. These are the connection strings that were used:

<connectionStrings>
  <add name="ApplicationDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=something;Integrated Security=True" providerName="System.Data.SqlClient" />
  <add name="ConnectionString" connectionString="Data Source=something;Persist Security Info=True;" providerName="Oracle.DataAccess.Client" />
</connectionStrings>

However, the database related to the model needs to be in Oracle (and I need a script for it but that is next weeks question) not SQL.

I was hoping that I could just change the data context provider name in the web config like so:

<connectionStrings>
  <add name="ApplicationDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=something;Integrated Security=True" providerName="System.Data.OracleClient" />
  <add name="ConnectionString" connectionString="Data Source=something;Persist Security Info=True;" providerName="Oracle.DataAccess.Client" />
</connectionStrings>

But that is not working - it gives me errors.

Please note that I am NOT asking about OracleMembershipProvider. This question is unique in that I want the model to relate to an Oracle database. I am not worried about membership and user authentication etc. I have already taken care of that.

It seems like it should be simple enough, but I have googled until every link I find is purple.

I need you to tell me the steps and what to do (hopefully in the web config) to get my model to 'create' an Oracle database. Please.

Upvotes: 0

Views: 6529

Answers (1)

abiNerd
abiNerd

Reputation: 2033

After reading through a ton of articles I found out that Oracle doesn't support code first (yet) and although there are some work-a-rounds it’s probably best to create scripts to create the db for us.

So as an alternate solution we to created the database in Oracle first and then connected to it from the MVC application.

We added an ADO.NET Entity Data Mode and in the EDM Wizard we said "Generated from database" and added a new connection to the Oracle database. You need Oracle Developer Tools for Visual Studio for this.

Add a new controller and for the data context class, choose Entities. Don't forget to add the connection string to the web config.

<add name="Entities" connectionString="...;provider=Oracle.DataAccess.Client;provider connection string=...;data source=...;" providerName="System.Data.EntityClient" />

This should be done before you start coding because you no longer have a model so I have had to change my code quite a lot. Not more than a day or two's work though.

Upvotes: 2

Related Questions