Dynamically change table schema in entity framework not working RRS feed

  • Question

  • User-1965208702 posted

    Dear Support Team,

    I am running one asp.net project where i used entityframework 6 Database first approach.

    I am using this context to just fetch the records from table.

    Now i have some requirement here:

    I have tableA which have default schema [dbo]. Now i have one more tableA having same structure with schema [MySchema] in same database.

    This table i need to call passing some validation.

    I used below code in Context class to get the specific table.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
                if (!string.IsNullOrEmpty(_currentEnvironment) && _currentEnvironment.ToLower() == "anotherenv")
                    modelBuilder.Entity<MENU_FUNCTIONS>().ToTable("MENU_FUNCTIONS", "ProviderAdmin");
                    modelBuilder.Entity<MENU_FUNCTIONS>().ToTable("MENU_FUNCTIONS", "dbo");
            public virtual DbSet<MENU_FUNCTIONS> MENU_FUNCTIONS { get; set; }

    But OnModelCreating method is not executing at all, Due to it specific table is not getting called.

    I did some googled and found like OnModelCreating method will call if use Code first approach.

    But in my case these two tables are already present in database, But i need to call specific schema entity based on above condition.

    How to achieve this?

    Please suggest.

    Monday, November 23, 2020 3:17 PM

All replies