You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法禁用Code First迁移:已设置初始化器仍报Oracle错误

Fix: ORA-00942 When Disabling Code First Migrations in Oracle

I've run into this exact issue with Oracle and Entity Framework before—setting Database.SetInitializer<MyContext>(null); doesn't always stop EF from poking around for migration-related tables. Let's break down why this is happening and how to fix it:

Why You're Seeing These Errors

Even with the initializer set to null, EF might still attempt to query __MIGRATIONHISTORY and EDMMETADATA tables for a few reasons:

  • Your AContext (a subclass of MyContext) isn't inheriting the initializer override properly—EF treats context subclasses as separate types with their own default initializers.
  • Old EF versions (EF5 or earlier) automatically check the EDMMETADATA table for model compatibility, regardless of initializer settings.
  • There might be a config file entry overriding your code-based initializer.

Step-by-Step Fixes

1. Explicitly Disable Initializer for Your Subclass Context

Since AContext is a separate context type, you need to set its initializer to null too. Update your AContext class:

public class AContext : MyContext {
    public AContext() : base() {
        // Ensure the subclass context also skips initialization
        Database.SetInitializer<AContext>(null);
    }

    public DbSet<YourEntity> YourEntities { get; set; }

    public override void OnModelCreating(DbModelBuilder modelBuilder) {
        modelBuilder.Configurations.Add(new Mapping());
    }
}

2. Add Explicit Initialization Block in Base Context

Modify your MyContext constructor to explicitly prevent EF from running initialization logic:

public class MyContext : DbContext {
    protected MyContext() : base("name=Entities") {
        Database.SetInitializer<MyContext>(null);
        // Tell EF not to initialize the database automatically
        Database.Initialize(false);
        // Disable model validation that might trigger metadata checks
        Configuration.ValidateOnSaveEnabled = false;
    }
}

3. Clean Up Your Config File

Check your web.config or app.config for any EF context configuration that might override your code. Remove any section like this:

<entityFramework>
    <contexts>
        <context type="YourNamespace.MyContext, YourAssembly">
            <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[YourNamespace.MyContext, YourAssembly], [YourNamespace.Migrations.Configuration, YourAssembly]], EntityFramework" />
        </context>
    </contexts>
</entityFramework>

4. Ignore Migration-Related Entities in Model Building

Force EF to ignore the migration history and EDM metadata tables by adding these lines to your OnModelCreating method (in either MyContext or AContext):

using System.Data.Entity.Migrations.History;

public override void OnModelCreating(DbModelBuilder modelBuilder) {
    // Ignore EF's internal migration tables
    modelBuilder.Ignore<MigrationHistory>();
    modelBuilder.Ignore<EdmMetadata>();
    
    // Your existing mapping config
    modelBuilder.Configurations.Add(new Mapping());
}

Final Notes

Oracle's ODP.NET provider can be finicky with Code First edge cases, so combining all these steps should stop EF from trying to access those missing tables. Make sure you're using the latest stable version of ODP.NET for Entity Framework too—older versions had more issues with migration disable logic.

内容的提问来源于stack exchange,提问作者McArthey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:21:37