2

I have Oracle database 11g and I want to use Entity Framework to read data from it. In first step I am trying to establish database connection and get status if it connected. However I am getting following error;

An exception of type 'System.ArgumentException' occurred in Oracle.ManagedDataAccess.dll but was not handled in user code

my connection string is as following;

 <connectionStrings>
    <add name="EBS_UCAS_DbConnection" providerName="Oracle.ManagedDataAccess.Client" connectionString="Data Source=(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 193.00.100.00)(PORT = 1500)))(CONNECT_DATA =(SERVICE_NAME = myservice)));Persist Security Info=True;User ID=myuser;Password=mypassword;Unicode=True"/>
</connectionStrings>

this is where I am trying to test code, where I am getting error

 public void TestOracleConnection()
    {
        using (var dbContext = new UCAS_dbContext())
        {
            var data = dbContext.SyncCodes.Select(x => x.SyncDesc).ToList();

            var xx = "dd";
        }
    }

Base DBContext class

public class BaseContext<TContext> : DbContext where TContext : DbContext
{
    static BaseContext()
    {
        Database.SetInitializer<TContext>(null);
    }

    protected BaseContext()
    : base("name = EBS_UCAS_DbConnection")
    { }
}

DbContext class

 public class UCAS_dbContext :BaseContext<UCAS_dbContext>
{
    public DbSet<SyncCodesEntity> SyncCodes { get; set; }
}

Model class

 [Table("SYNC_CODES")]
public class SyncCodesEntity
{
    [Key]
    public int ID { get; set; }

    public string SyncDesc { get; set; }
}

In other project where TNS is working

 public string database = "(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 193.00.100.00)(PORT = 1500)))(CONNECT_DATA =(SERVICE_NAME = myserver)))";

   public void setConnectString(string username, string password, string database)
    {

        if (this.disposed)
        {
            throw new ObjectDisposedException("clarusOracleObj");
        }
        connectString = "Data Source=" + database + ";Persist Security Info=True;User ID=" + username + ";Password=" + password;
        try
        {
            dbConn = new OracleConnection(connectString);
            dbConn.Open();
            dbConn.Close();
        }
        catch (Exception ex)
        {
            //reset the connectString and throw the exception again.
            connectString = "";
            throw ex;
        }
    }

Connection string copy from data property in visual studio and I use .NET Framework Data Provider for Oracle

 Data Source=sittingbull.blackpool.ac.uk:1500/myservice;Persist Security Info=True;User ID=myuser;Password=***********;Unicode=True
29
  • First question is what have you got installed re the oracle client on the machine your using Commented Nov 11, 2016 at 8:28
  • are you referencing your TNSNames file correctly and whats in your ODBC \ Datasources manager Commented Nov 11, 2016 at 8:32
  • also, this might be of some use to you oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/NuGet/… Commented Nov 11, 2016 at 8:32
  • Possible duplicate of .net entity framework with oracle 11g Commented Nov 11, 2016 at 8:33
  • didn't mean to bombard you :) Commented Nov 11, 2016 at 8:34

2 Answers 2

2

after long battle found the answer of my issue.

Because I am using in WPF application so in App.Config file define the data Source alias and TNS detail to oracle database that you are trying to read ;

App.Config

<oracle.manageddataaccess.client>
 <version number="*">
  <dataSources>
    <dataSource alias="DefaultDataSource" descriptor="(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 190.00.100.00)(PORT = 1500)))(CONNECT_DATA =(SERVICE_NAME = myserver)))"/>
  </dataSources>
 </version>
</oracle.manageddataaccess.client>

and point the data source alias in connection string with username and password

<connectionStrings>
  <add name="OracleDbContext" providerName="Oracle.ManagedDataAccess.Client" connectionString="Data Source=DefaultDataSource;User ID=myusername;Password=mypass;"/>
</connectionStrings>

so now you can use OracleDbContext with DbContext class to reference this database;

In doing this implementation I have come across another error

Solving System.Data.Entity.Core.EntityCommandExecutionException

System.Data.Entity.Core.EntityCommandExecutionException' with InnerException: ORA-00942 table or view does not exist

this error happened to due to wrong schema pick by default, I believe it pick dbo where in my case schema is ucas, so I need to define schema in onModelCreating override method

 public class UCAS_dbContext :BaseContext<UCAS_dbContext>
{

    public DbSet<SyncCodesEntity> SyncCodes { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("UCAS");
    }
}

and finally my test Connection class

Test Connection

 public void TestOracleConnection()
    {
        using (var dbContext = new UCAS_dbContext())
        {
            var query = (from b in dbContext.SyncCodes
                         select b).ToList();

        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

One problem could be the spaces in your TNS. Enclose it by double-quotes, i.e. try this:

<connectionStrings>
    <add 
       name="EBS_UCAS_DbConnection" 
       providerName="Oracle.ManagedDataAccess.Client" 
       connectionString='"Data Source=(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 193.00.100.00)(PORT = 1500)))(CONNECT_DATA =(SERVICE_NAME = myservice)))";Persist Security Info=True;User ID=myuser;Password=mypassword;Unicode=True'/>
</connectionStrings>

or

<connectionStrings>
    <add 
       name="EBS_UCAS_DbConnection" 
       providerName="Oracle.ManagedDataAccess.Client" 
       connectionString="&quot;Data Source=(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 193.00.100.00)(PORT = 1500)))(CONNECT_DATA =(SERVICE_NAME = myservice)))&quot;;Persist Security Info=True;User ID=myuser;Password=mypassword;Unicode=True"/>
</connectionStrings>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.