0

I have searched the web and reviewed similar questions on Stack Overflow but I can not figure how to use the Oracle Managed Data Provider with a DbContext from a C# .Net 4.6 Console Application.

This works for Sql Express

class SqlDBContext1 : DbContext
{
    public SqlDBContext1() : base("SqlExpressDB")
    {
        using (var ctx = this)
        { var query = from c_codes in ctx.CountryCodes select c_codes; }
    }
    public DbSet<CountryCode> CountryCodes { get; set; }
}

And this works to connect to a remote Sql Server, using a connection string

class SqlDBContext2 : DbContext
{
    public SqlDBContext2() : base(SqlServerString())
    {
        using (var ctx = this)
        { var query = from c_codes in ctx.CountryCodes select c_codes; }
    }
    public DbSet<CountryCode> CountryCodes { get; set; }
    private static string SqlServerString()
    {
        return @"Data Source=192.168.0.1;" +
                "Initial Catalog=SqlDB1; " +
                "Persist Security Info=True; " +
                "User ID=USER1; " +
                "Password=PASS1";
    }
}

I can connect to Oracle from Visual Studio 2017 and I have verified the connection string using a standard OracleConnection, it opens and closes, no problem.

With this in the APP.CONFIG

  <oracle.manageddataaccess.client>
    <version number="*">
      <dataSources>
        <dataSource alias="ORCL1" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.0.1)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL1))) "/>
      </dataSources>
    </version>
  </oracle.manageddataaccess.client>
  <connectionStrings>
    <add name="ORCL1DB" providerName="Oracle.ManagedDataAccess.Client" connectionString="User Id=USER1;Password=PASS1;Data Source=ORCL1"/>
  </connectionStrings>

Can I use something like this? If so, what goes into the DbContext's base contructor?

class OraDBContext : DbContext
{
    public OraDBContext() : base("What goes here")
    {
        using (var ctx = this)
        { var query = from c_codes in ctx.CountryCodes select c_codes; }
    }
    public DbSet<CountryCode> CountryCodes { get; set; }
}

None of the examples I have found on Stack Overflow work, various errors of which 'Type Initializor threw an exception' being the most popular.

1 Answer 1

1

Not sure what I did different but now it works, for future generations.

In APP.CONFIG I have this

  <oracle.manageddataaccess.client>
    <version number="*">
      <dataSources>
        <dataSource alias="ORCL1" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.0.1)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL1))) "/>
      </dataSources>
    </version>
  </oracle.manageddataaccess.client>
  <connectionStrings>
    <add name="ORCL1DB" providerName="Oracle.ManagedDataAccess.Client" connectionString="User Id=USER1;Password=PASS1;Data Source=ORCL1"/>
  </connectionStrings>

My class looks like this

class OraDBContext : DbContext
{
    public OraDBContext() : base(ORCL1COnnection(), true)
    {
        using (var ctx = this)
        {
            var query = from c_codes in ctx.CountryCodes select c_codes;
        }
    }

    public DbSet<CountryCode> CountryCodes { get; set; }

    public static OracleConnection ORCL1COnnection()
    {
        var c1 = ConfigurationManager.ConnectionStrings["ORCL1DB"];
        OracleConnection ora_con = new OracleConnection(c1.ConnectionString);
        return ora_con;
    }
}

This was failing before but is working now.

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

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.