Oracle.ManagedDataAccess.EntityFramework - ORA-01918:用户'dbo'不存在

时间:2014-12-02 13:22:18

标签: oracle entity-framework code-first ef-migrations odp.net

我正在尝试使用Oracle.ManagedDataAccess 6.121.1.0提供程序实现代码First Migrations,但根本没有成功。

当我收到ORA代码时,我假设连接已成功打开。但是迁移失败了,因为可能提供者表现为SQL Server而不是Oracle。我认为这是因为它使用'dbo'作为默认架构。

这是我的web.config设置:

<configuration>
  <configSections>
   <section name="entityFramework"
             type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
             requirePermission="false" />
    <section name="Oracle.ManagedDataAccess.Client"
             type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
  </configSections>
  <entityFramework>
    <contexts>
      <context type="MyProject.Context.MainContext, MyProject.Context">
        <databaseInitializer type="MyProject.Context.Config.ContextInitializer, MyProject.Context" />
      </context>
    </contexts>
    <defaultConnectionFactory type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess" />    
    <providers>    
      <provider invariantName="Oracle.ManagedDataAccess.Client" 
                type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="Oracle.ManagedDataAccess.Client" />
      <add name="ODP.NET, Managed Driver"
           invariant="Oracle.ManagedDataAccess.Client"
           description="Oracle Data Provider for .NET, Managed Driver"
           type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="MainContext"
         providerName="Oracle.ManagedDataAccess.Client"
         connectionString="Data Source=OracleServer:1521/BRSYSDS;User ID=USER;Password=PASSWORD;" />
  </connectionStrings>
  <!-- other settings -->
</configuration>

这里是Stacktrace:

  

[OracleException(0x77e):ORA-01918:用户'dbo'不存在]
  OracleInternal.ServiceObjects.OracleCommandImpl.VerifyExecution(OracleConnectionImpl connectionImpl,Int32&amp; cursorId,Boolean bThrowArrayBindRelatedErrors,OracleException&amp; exceptionForArrayBindDML,Boolean&amp; hasMoreRowsInDB,Boolean bFirstIterationDone)+652   OracleInternal.ServiceObjects.OracleCommandImpl.VerifyExecution(OracleConnectionImpl connectionImpl,Int32&amp; cursorId,Boolean bThrowArrayBindRelatedErrors,OracleException&amp; exceptionForArrayBindDML,Boolean bFirstIterationDone)+39
  OracleInternal.ServiceObjects.OracleCommandImpl.ExecuteNonQuery(字符串的CommandText,OracleParameterCollection paramColl,的CommandType命令类型,OracleConnectionImpl connectionImpl,的Int32 longFetchSize,Int64的clientInitialLOBFS,OracleDependencyImpl orclDependencyImpl,Int64的[]&安培; scnFromExecution,OracleParameterCollection&安培; bindByPositionParamColl,布尔&安培; bBindParamPresent,OracleException&安培; exceptionForArrayBindDML,布尔isFromEF )+7480
  Oracle.ManagedDataAccess.Client.OracleCommand.ExecuteNonQuery()+678
  System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.b__0(DbCommand t,DbCommandInterceptionContext 1 c) +10
System.Data.Entity.Infrastructure.Interception.InternalDispatcher
1.Dispatch(TTarget target,Func 3 operation, TInterceptionContext interceptionContext, Action 3执行,Action 3 executed) +72
System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext) +357
System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery() +104
System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement, DbInterceptionContext interceptionContext) +152
System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable
1 migrationStatements,DbTransaction事务,DbInterceptionContext interceptionContext)+82
  System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable 1 migrationStatements, DbConnection connection) +626
System.Data.Entity.Migrations.<>c__DisplayClass30.<ExecuteStatements>b__2e() +19
System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute(Action operation) +9
System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable
1 migrationStatements,DbTransaction existingTransaction)+194
  System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable 1 migrationStatements) +7
System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable
1个操作,IEnumerable 1 systemOperations, Boolean downgrading, Boolean auto) +825
System.Data.Entity.Migrations.DbMigrator.AutoMigrate(String migrationId, VersionedModel sourceModel, VersionedModel targetModel, Boolean downgrading) +564
System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable
1 pendingMigrations,String targetMigrationId,String lastMigrationId)+404
  System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)+447
  System.Data.Entity.Migrations。&lt;&gt; c__DisplayClassc.b__b()+ 13
  System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)+422
  System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)+78
  System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext,Func 3 createMigrator, ObjectContext objectContext) +89
System.Data.Entity.Internal.InternalContext.CreateDatabase(ObjectContext objectContext, DatabaseExistenceState existenceState) +116
System.Data.Entity.Database.Create(DatabaseExistenceState existenceState) +218
System.Data.Entity.DropCreateDatabaseAlways
1.InitializeDatabase(TContext context)+137

8 个答案:

答案 0 :(得分:37)

我遇到了同样的问题,Thiago Lunardi的回答解决了这个问题。谢谢。我没有足够的声誉来表达你的回复。在这里提一下,我在UPPERCASE中设置模式名称后成功了。

将它放在新dbContext类下的Context文件中,如下所示:

public partial class MyAppContext : DbContext
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.HasDefaultSchema("UPPERCASE_SCHEMA_NAME");
...

答案 1 :(得分:16)

我只是在modelBuilder

中设置默认架构来解决这个问题
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.HasDefaultSchema("MyOracleSchema");

    // ...
}

答案 2 :(得分:2)

设置默认架构对我不起作用。 我通过自定义迁移历史记录表来设置不同的架构,从而找到了解决方案。

您可以在此处找到解决方案:LINK

答案 3 :(得分:2)

如果缺少表格的完全限定名称,用户Dbo也会出现。这可能无法映射到数据库中的右表。

enter image description here

答案 4 :(得分:1)

如果您使用自动迁移(就像我一样),请注意:modelBuilder.HasDefaultSchema在您切换到显式迁移之前不会有任何帮助。

来自Oracle Docs

代码优先自动迁移仅限于使用 dbo 架构。由于此限制,建议使用基于代码的迁移,即通过Add-Migration命令添加显式迁移

答案 5 :(得分:1)

在Code First中,您可以使用Table的DataAnnotations。

[表格(“Emplpoyee”,Schema =“您的计划名称”]

答案 6 :(得分:0)

我遇到了同样的问题。我将模式名称放在 OnModelCreating() 方法中。

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

    // ...
}

但是,以大写形式设置架构名称对我不起作用。我在 Confifuration.cs 中添加了下面提到的代码,它起作用了!!

转到迁移 -> Configuration.cs

 class Configuration : DbMigrationsConfiguration<CodeFirstOracleProject.Context>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        var historyContextFactory = GetHistoryContextFactory("Oracle.ManagedDataAccess.Client");
        SetHistoryContextFactory("Oracle.ManagedDataAccess.Client",
                                 (dbc, schema) => historyContextFactory.Invoke(dbc, "YourSchemaName"));
    }
}

答案 7 :(得分:0)

在我的情况下,以大写形式编写模式名称是不够的,我不得不使用 toUpper() 函数:

 modelBuilder.HasDefaultSchema("YOURSCHEMA".ToUpper())

同时添加

   public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        var historyContextFactory = GetHistoryContextFactory("Oracle.ManagedDataAccess.Client");
        SetHistoryContextFactory("Oracle.ManagedDataAccess.Client",
                                 (dbc, schema) => historyContextFactory.Invoke(dbc, "YOURSCHEMA".ToUpper()));
    }

删除迁移并重新生成它们解决了问题。