title: Connect Oracle DB with Entity Framework with Code First in VS2022
body: <p>I need to connect with oracle database using entity framework in my MVC WebAPI project. I am using VS2022 with .Net Framework 4.8. I prefer working with code first approach and need to auto create the Database and Tables if not exists.</p>
  <p>The packages I have added in my project for oracle are
  <a href="https://i.stack.imgur.com/vCA4v.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vCA4v.png" alt="enter image description here" /></a></p>
  <p>My web.config of the project is as below</p>
  <pre><code>    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;configuration&gt;
    &lt;configSections&gt;
      &lt;section name=&quot;oracle.manageddataaccess.client&quot; type=&quot;OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.21.1, Culture=neutral, PublicKeyToken=89b483f429c47342&quot; /&gt;
      &lt;!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --&gt;
      &lt;section name=&quot;entityFramework&quot; type=&quot;System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&quot; requirePermission=&quot;false&quot; /&gt;
    &lt;/configSections&gt;
    &lt;oracle.manageddataaccess.client&gt;
      &lt;version number=&quot;*&quot;&gt;
        &lt;dataSources&gt;
          &lt;!-- Customize these connection alias settings to connect to Oracle DB --&gt;
          &lt;dataSource alias=&quot;OracleDataSource&quot; descriptor=&quot;(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=175.16.2.1)(PORT=1521))(CONNECT_DATA=(SERVER = DEDICATED)(SERVICE_NAME=oracledb12c))) &quot; /&gt;
        &lt;/dataSources&gt;
      &lt;/version&gt;
    &lt;/oracle.manageddataaccess.client&gt;
    &lt;connectionStrings&gt;
      &lt;add name=&quot;OracleDbContext&quot; providerName=&quot;Oracle.ManagedDataAccess.Client&quot; connectionString=&quot;User Id=oracle_user;Password=oracle_user_password;Data Source=OracleDataSource&quot; /&gt;
    &lt;/connectionStrings&gt;
    &lt;appSettings&gt;
      &lt;add key=&quot;webpages:Version&quot; value=&quot;3.0.0.0&quot; /&gt;
      &lt;add key=&quot;webpages:Enabled&quot; value=&quot;false&quot; /&gt;
      &lt;add key=&quot;ClientValidationEnabled&quot; value=&quot;true&quot; /&gt;
      &lt;add key=&quot;UnobtrusiveJavaScriptEnabled&quot; value=&quot;true&quot; /&gt;
    &lt;/appSettings&gt;
    &lt;system.web&gt;
      &lt;compilation debug=&quot;true&quot; targetFramework=&quot;4.8&quot; /&gt;
      &lt;httpRuntime targetFramework=&quot;4.8&quot; /&gt;
    &lt;/system.web&gt;
    &lt;system.webServer&gt;
      &lt;handlers&gt;
        &lt;remove name=&quot;ExtensionlessUrlHandler-Integrated-4.0&quot; /&gt;
        &lt;remove name=&quot;OPTIONSVerbHandler&quot; /&gt;
        &lt;remove name=&quot;TRACEVerbHandler&quot; /&gt;
        &lt;add name=&quot;ExtensionlessUrlHandler-Integrated-4.0&quot; path=&quot;*.&quot; verb=&quot;*&quot; type=&quot;System.Web.Handlers.TransferRequestHandler&quot; preCondition=&quot;integratedMode,runtimeVersionv4.0&quot; /&gt;
      &lt;/handlers&gt;
    &lt;/system.webServer&gt;
    &lt;runtime&gt;
      &lt;assemblyBinding xmlns=&quot;urn:schemas-microsoft-com:asm.v1&quot;&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name=&quot;Antlr3.Runtime&quot; publicKeyToken=&quot;eb42632606e9261f&quot; /&gt;
          &lt;bindingRedirect oldVersion=&quot;0.0.0.0-3.5.0.2&quot; newVersion=&quot;3.5.0.2&quot; /&gt;
        &lt;/dependentAssembly&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name=&quot;Newtonsoft.Json&quot; culture=&quot;neutral&quot; publicKeyToken=&quot;30ad4fe6b2a6aeed&quot; /&gt;
          &lt;bindingRedirect oldVersion=&quot;0.0.0.0-12.0.0.0&quot; newVersion=&quot;12.0.0.0&quot; /&gt;
        &lt;/dependentAssembly&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name=&quot;System.Web.Optimization&quot; publicKeyToken=&quot;31bf3856ad364e35&quot; /&gt;
          &lt;bindingRedirect oldVersion=&quot;1.0.0.0-1.1.0.0&quot; newVersion=&quot;1.1.0.0&quot; /&gt;
        &lt;/dependentAssembly&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name=&quot;WebGrease&quot; publicKeyToken=&quot;31bf3856ad364e35&quot; /&gt;
          &lt;bindingRedirect oldVersion=&quot;0.0.0.0-1.6.5135.21930&quot; newVersion=&quot;1.6.5135.21930&quot; /&gt;
        &lt;/dependentAssembly&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name=&quot;System.Web.Helpers&quot; publicKeyToken=&quot;31bf3856ad364e35&quot; /&gt;
          &lt;bindingRedirect oldVersion=&quot;1.0.0.0-3.0.0.0&quot; newVersion=&quot;3.0.0.0&quot; /&gt;
        &lt;/dependentAssembly&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name=&quot;System.Web.WebPages&quot; publicKeyToken=&quot;31bf3856ad364e35&quot; /&gt;
          &lt;bindingRedirect oldVersion=&quot;1.0.0.0-3.0.0.0&quot; newVersion=&quot;3.0.0.0&quot; /&gt;
        &lt;/dependentAssembly&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name=&quot;System.Web.Mvc&quot; publicKeyToken=&quot;31bf3856ad364e35&quot; /&gt;
          &lt;bindingRedirect oldVersion=&quot;1.0.0.0-5.2.7.0&quot; newVersion=&quot;5.2.7.0&quot; /&gt;
        &lt;/dependentAssembly&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name=&quot;System.Runtime.CompilerServices.Unsafe&quot; publicKeyToken=&quot;b03f5f7f11d50a3a&quot; culture=&quot;neutral&quot; /&gt;
          &lt;bindingRedirect oldVersion=&quot;0.0.0.0-6.0.0.0&quot; newVersion=&quot;6.0.0.0&quot; /&gt;
        &lt;/dependentAssembly&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name=&quot;System.Text.Json&quot; publicKeyToken=&quot;cc7b13ffcd2ddd51&quot; culture=&quot;neutral&quot; /&gt;
          &lt;bindingRedirect oldVersion=&quot;0.0.0.0-6.0.0.0&quot; newVersion=&quot;6.0.0.0&quot; /&gt;
        &lt;/dependentAssembly&gt;
        &lt;dependentAssembly&gt;
          &lt;publisherPolicy apply=&quot;no&quot; /&gt;
          &lt;assemblyIdentity name=&quot;Oracle.ManagedDataAccess&quot; publicKeyToken=&quot;89b483f429c47342&quot; culture=&quot;neutral&quot; /&gt;
          &lt;bindingRedirect oldVersion=&quot;4.122.0.0 - 4.65535.65535.65535&quot; newVersion=&quot;4.122.21.1&quot; /&gt;
        &lt;/dependentAssembly&gt;
      &lt;/assemblyBinding&gt;
    &lt;/runtime&gt;
    &lt;system.codedom&gt;
      &lt;compilers&gt;
        &lt;compiler language=&quot;c#;cs;csharp&quot; extension=&quot;.cs&quot; type=&quot;Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35&quot; warningLevel=&quot;4&quot; compilerOptions=&quot;/langversion:default /nowarn:1659;1699;1701&quot; /&gt;
        &lt;compiler language=&quot;vb;vbs;visualbasic;vbscript&quot; extension=&quot;.vb&quot; type=&quot;Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35&quot; warningLevel=&quot;4&quot; compilerOptions=&quot;/langversion:default /nowarn:41008 /define:_MYTYPE=\&amp;quot;Web\&amp;quot; /optionInfer+&quot; /&gt;
      &lt;/compilers&gt;
    &lt;/system.codedom&gt;
    &lt;system.data&gt;
      &lt;DbProviderFactories&gt;
        &lt;remove invariant=&quot;Oracle.ManagedDataAccess.Client&quot; /&gt;
        &lt;add name=&quot;ODP.NET, Managed Driver&quot; invariant=&quot;Oracle.ManagedDataAccess.Client&quot; description=&quot;Oracle Data Provider for .NET, Managed Driver&quot; type=&quot;Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.122.21.1, Culture=neutral, PublicKeyToken=89b483f429c47342&quot; /&gt;
      &lt;/DbProviderFactories&gt;
    &lt;/system.data&gt;
    &lt;entityFramework&gt;
      &lt;providers&gt;
        &lt;provider invariantName=&quot;System.Data.SqlClient&quot; type=&quot;System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer&quot; /&gt;
        &lt;provider invariantName=&quot;Oracle.ManagedDataAccess.Client&quot; type=&quot;Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.122.21.1, Culture=neutral, PublicKeyToken=89b483f429c47342&quot; /&gt;
      &lt;/providers&gt;
    &lt;/entityFramework&gt;
  &lt;/configuration&gt;
  </code></pre>
  <p>My DBContext class and model class are</p>
  <pre><code>public class OracleDbContext : DbContext
      {
          public OracleDbContext() : base(&quot;OracleDbContext&quot;)
          {
              
          }
  
          public DbSet&lt;User&gt; Users { get; set; }
      }
  
  public class User
      {
          public int UserId { get; set; }
          public string UserName { get; set; }
      }
  </code></pre>
  <p>After all this when I run add-migration m1, it runs successfully and created its cs file. After that when I run Update-Database, it is giving me an exception and DB not created. The exception I am getting is as follows</p>
  <pre><code>Oracle.ManagedDataAccess.Client.OracleException (0x80004005): ORA-01918: user 'dbo' does not exist
     at OracleInternal.ServiceObjects.OracleConnectionImpl.VerifyExecution(Int32&amp; cursorId, Boolean bThrowArrayBindRelatedErrors, SqlStatementType sqlStatementType, Int32 arrayBindCount, OracleException&amp; exceptionForArrayBindDML, Boolean&amp; hasMoreRowsInDB, Boolean bFirstIterationDone)
     at OracleInternal.ServiceObjects.OracleCommandImpl.ExecuteNonQuery(String commandText, OracleParameterCollection paramColl, CommandType commandType, OracleConnectionImpl connectionImpl, Int32 longFetchSize, Int64 clientInitialLOBFS, OracleDependencyImpl orclDependencyImpl, Int64[]&amp; scnFromExecution, OracleParameterCollection&amp; bindByPositionParamColl, Boolean&amp; bBindParamPresent, OracleException&amp; exceptionForArrayBindDML, OracleConnection connection, Boolean isFromEF)
     at Oracle.ManagedDataAccess.Client.OracleCommand.ExecuteNonQuery()
     at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
     at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext)
     at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(MigrationStatement migrationStatement, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext)
     at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(MigrationStatement migrationStatement, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext)
     at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext)
     at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinTransaction(IEnumerable`1 migrationStatements, DbTransaction transaction, DbInterceptionContext interceptionContext)
     at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinNewTransaction(IEnumerable`1 migrationStatements, DbConnection connection, DbInterceptionContext interceptionContext)
     at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection, DbInterceptionContext interceptionContext)
     at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection)
     at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements, DbTransaction existingTransaction)
     at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, VersionedModel targetModel, IEnumerable`1 operations, IEnumerable`1 systemOperations, Boolean downgrading, Boolean auto)
     at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
     at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
     at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
     at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
     at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
     at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
     at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
     at System.Data.Entity.Infrastructure.Design.Executor.Update.&lt;&gt;c__DisplayClass0_0.&lt;.ctor&gt;b__0()
     at System.Data.Entity.Infrastructure.Design.Executor.OperationBase.Execute(Action action)
  ORA-01918: user 'dbo' does not exist
  </code></pre>
  <p>So, I don't understand why this user not exist error is coming. Any help regarding this will be beneficial.</p>