Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9254695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:32:11+00:00 2026-06-18T11:32:11+00:00

I have two tables that have the same layout – Report Table ID ReportCol1

  • 0

I have two tables that have the same layout –

Report Table
   ID
   ReportCol1
   ReportCol2

In another database I have

 Reporting Table
    ID
    ReportCol1
    ReportCol2

I want to use a single entity model called Report to load the data from both of these tables.

In my context class I have

public DbSet<Report> Report{ get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new ReportMap());
}

In my call to the first database Report table I get the results as expected.

I change the connection string to point to the second database, but I can’t change the name of the table in the table mapping.

I don’t want to use stored procs for the reason outlined in my comment.

What can I do, short of the tables names in the database(that is not an option).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:32:12+00:00Added an answer on June 18, 2026 at 11:32 am

    Have you tried this fluent API modelBuilder.Entity<Report>().ToTable("Reporting"); ? You may need to write this so it conditionally does this based on which database you are connecting to. You may need to have your configuration allow you to say “DatabaseA uses this mapping and connection string”, and “DatabaseB uses this other mapping and conenctions string”, and rather than changing the connection string, you specify which database by some name/key, and your app looks up that name to determine which mapping code to run.

    if(dbMappingconfig == DbMapping.A)//some enum you create
    {
        modelBuilder.Entity<Report>().ToTable("Reporting");
    }
    

    If your goal is to be able to pass these entities to other methods like DisplayReport(Report r) so that you don’t have to duplicate code, you could have both Reporting and Report classes implement a IReport interface.

    EF also supports inheritance hierarchies, so you could have them inherit from the same class, BUT I havfe a strong feeling that will not work across databases.

    If the OnModelCreating doesn’t rerun, it’s probably already cached. Put modelBuilder.CacheForContextType = false; in there so it doesn’t cache it in future, and to clear the current cache I think you can just do a Clean+Rebuild. This will come at the price of rebuilding the model everytime instead of reusing a cache. What you’d really want is use the cache up until the connection string changes. I don’t know of anyway to manually clear the cache, but there might be a way. You can manage the model building yourself:

    DbModelBuilder builder = new DbModelBuilder();
    // Setup configurations
    DbModel model = builder.Build(connection);
    DbCompiledModel compiledModel = model.Compile();
    DbContext context = new DbContext(connection, compiledModel);
    

    But that will introduce additional complexities since you will need to manage the caching yourself.

    While searching on this, I came across this that looks like they are trying to accomplish the same thing, as well as having gone down the same page, see Final section in question: How to map an Entity framework model to a table name dynamically

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two separate sets of tables in the same database that model the
I have data in two tables that I want to query from. Each table
I have two tables that have the exact same fields and I want to
I have two tables that have the exact same structure. Table MasterList Acct_id(9) Name
I have two tables, A and B, that have the same structure (about 30+
I have two tables that I want to join together. Table1 Year, ID, Theme,
I have two tables that are related via a mapping table: keywords titles I
I have two tables that I need to join... I want to join table1
I have two tables that share the same column ( C ) but SQL
I have two tables that contain completely separate information: table: tires columns: Tire_id name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.