How to use DbConnection in DbContext — oracle-tech

    Forum Stats

  • 3,716,136 Users
  • 2,242,961 Discussions
  • 7,845,842 Comments

Discussions

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to use DbConnection in DbContext

user4524874
user4524874 Member Posts: 2
edited August 2019 in ODP.NET

I'm working in ASP.NET 4.5 project using Entity Framework 6 Database First approach.

I need to use a OracleConnection into DbContext because I can't use a connection string to establish the communication with the DBMS.

I know DbContext class has a constructor accepting an instance of DbConnection but I having some problems with it.

Do you know where I can get more information about this issue?

Thanks.

Answers

  • Alex Keh-Oracle
    Alex Keh-Oracle Posts: 2,753 Employee
    edited August 2019

    What is the exact problem?

  • user4524874
    user4524874 Member Posts: 2
    edited August 2019

    When I try to execute a LINQ expression using the DbContext instance I got using the constructor with a OracleConnection object I get the following exception:

    NameValueType
    $exception{"Unable to determine the provider name for provider factory of type 'Oracle.ManagedDataAccess.Client.OracleClientFactory'. Make sure that the ADO.NET provider is installed or registered in the application config."}System.NotSupportedException

    The program works fine when I use the DbContext constructor with a parameter that uses a connection string specified in the Web.config.

    NameValueType
    StackTrace

    " at System.Data.Entity.Utilities.DbProviderFactoryExtensions.GetProviderInvariantName(DbProviderFactory factory) at System.Data.Entity.Infrastructure.DependencyResolution.DefaultInvariantNameResolver.GetService(Type type, Object key) at System.Data.Entity.Infrastructure.DependencyResolution.CachingDependencyResolver.<>c__DisplayClass1.<GetService>b__0(Tuple`2 k) at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) at System.Data.Entity.Infrastructure.DependencyResolution.CachingDependencyResolver.GetService(Type type, Object key) at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass3.<GetService>b__0(IDbDependencyResolver r) at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate) at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key) at System.Data.Entity.Infrastructure.DependencyResolution.RootDependencyResolver.GetService(Type type, Object key) at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass3.<GetService>b__0(IDbDependencyResolver r) at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate) at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key) at System.Data.Entity.Infrastructure.DependencyResolution.CompositeResolver`2.GetService(Type type, Object key) at System.Data.Entity.Infrastructure.DependencyResolution.DbDependencyResolverExtensions.GetService[T](IDbDependencyResolver resolver, Object key) at System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInvariantName(DbConnection connection) at System.Data.Entity.Internal.InternalConnection.get_ProviderName() at System.Data.Entity.Internal.LazyInternalContext.get_ProviderName() at System.Data.Entity.Internal.DefaultModelCacheKeyFactory.Create(DbContext context) at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() at System.Data.Entity.Internal.InternalContext.Initialize() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() at System.Linq.Lookup`2.CreateForJoin(IEnumerable`1 source, Func`2 keySelector, IEqualityComparer`1 comparer) at System.Linq.Enumerable.<JoinIterator>d__38`4.MoveNext() at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext() at System.Linq.Lookup`2.CreateForJoin(IEnumerable`1 source, Func`2 keySelector, IEqualityComparer`1 comparer) at System.Linq.Enumerable.<JoinIterator>d__38`4.MoveNext() at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext() at

    ….

    I suppose I have to specify the data provider name somewhere but I don't find more information about this case where you use a DbConnection object anywhere.

    Thanks.

  • Alex Keh-Oracle
    Alex Keh-Oracle Posts: 2,753 Employee
    edited August 2019

    This error is generally a setup config or install issue. Can you describe how you installed and setup ODP.NET and the Oracle EF provider in your project?

    The other way to approach this is to use one of the existing EF6 tutorials, which covers install and setup. Here's the Code First tutorial:

    https://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/2015/codefirstandcodefirstmigrations/Entity%20Frame…

    In case you need to know what DB First setup is like, here's that tutorial:

    https://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/2015/entityframework_linq_modelfirst/Entity%20Frame…

Sign In or Register to comment.