3

I am trying to use Oracle using dotConnect for Oracle(v 9.4). Since aspnet core 2.0 has full support to reference older .net framework, I am trying to connect to database using a regular .net assembly(i.e assembly containing my DbContext is in targeting .net 4.6.1 and is been referred in my asp.net core 2 project).

I am facing the below issue

"System.TypeInitializationException: 'The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.'

Inner Exception: FileNotFoundException: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified."

Here is my CodeConfig class

public class CodeConfig : DbConfiguration
{
        public CodeConfig()
        {
            SetProviderServices("Devart.Data.Oracle", Devart.Data.Oracle.Entity.OracleEntityProviderServices.Instance);
            SetProviderFactory("Devart.Data.Oracle", new Devart.Data.Oracle.OracleProviderFactory());
        }
}

My DbContext class

[DbConfigurationType(typeof(CodeConfig))]
public partial class MyEntities : DataContext, IMyStoredProcedures
{
  public MyEntities(string nameOrConnectionString) : base(nameOrConnectionString)
        {
            Configure();
        }

        private void Configure()
        {
            this.Configuration.AutoDetectChangesEnabled = true;
            this.Configuration.LazyLoadingEnabled = true;
            this.Configuration.ProxyCreationEnabled = true;
            this.Configuration.ValidateOnSaveEnabled = true;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }    

    }
...
}

Exception is thrown when base(nameOrConnectionString) is executed.

DI in startup

var dbContext = new MyEntities(Configuration["Data:DefaultConnection:ConnectionString"]);
services.AddScoped<IDataContextAsync>(provider => dbContext);
services.AddScoped<IMyStoredProcedures>(provider => dbContext);

Appsettings.json

"Data": {
    "DefaultConnection": {
      "ConnectionString": "metadata=res://Org.MyApp.Entities/MyModel.csdl|res://Org.MyApp.Entities/MyModel.ssdl|res://Org.MyApp.Entities/MyModel.msl;provider=Devart.Data.Oracle;provider connection string=&quot;User Id=UID;Password=****;Server=Server;Persist Security Info=True&quot;"
    }
  }

The following links are used as reference.

  1. Getting started Asp.net core with EF6

  2. aspnet core 1.x with EF6(Based on 1.x but has useful information)

int-i
  • 661
  • 1
  • 12
  • 28

1 Answers1

2

After many tries, I finally was able to crack through this.

  1. quot; in connection string should be replaced with '
  2. Instead of targeting netcoreapp2.0, target your aspnetcore 2.0 application to net461 or any full framework higher than that (Look for net461 in your aspnetcore 2.0 csproj file)
  3. Add EF >=6.1 from nuget to your modal/entities assembly. Reference Devart.Data, DevArt.Data.Oracle. Devart.Data.Oracle.Entoty.EF6 in the same assembly.
  4. Create a class OracleDbConfiguration Derived from DevArt.Data.Entity.OracleEntityProviderServicesConfiguration
public class OracleDbConfiguration : OracleEntityProviderServicesConfiguration// : DbConfiguration
{
    public OracleDbConfiguration()
    {
         SetProviderServices("Devart.Data.Oracle", OracleEntityProviderServices.Instance);
         SetProviderFactory("Devart.Data.Oracle", OracleProviderFactory.Instance);
    }
}
  1. Decorate your DbContext class with it. E.g.

    [DbConfigurationType(typeof(OracleDbConfiguration))]

  2. Create a static constructor on your DbContext class and fill it like this

static MyEntities()
{
    var config = Devart.Data.Oracle.Entity.Configuration.OracleEntityProviderConfig.Instance;
    config.CodeFirstOptions.ColumnTypeCasingConventionCompatibility = false;
}

When I get a chance, I will upload a simple app which has the complete solution.

int-i
  • 661
  • 1
  • 12
  • 28
  • 1
    This is exactly what I needed. I didn't have to do anything involving Devart, but some combination of using `'` over `"` in the connection string and targeting `net461` with my .Net Core MVC app seems to have done the trick. Thank you!!! – neilsimp1 Nov 01 '18 at 15:51