Take the 2-minute tour ×

I have two tables that have the same layout -

Report Table

In another database I have

 Reporting Table

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).

share|improve this question
You could write two SP, one in each DB with the same name and then call that to get your results –  glosrob Feb 6 '13 at 21:11
I'm hoping to do this entirely from code, there are in fact about 50 databases and in the end there will be many tables with the same format but a different name. –  tom Feb 6 '13 at 21:12
can you pass the table name into the context as a parameter, and set it in the mapping like ToTable( myVariable ); –  Forty-Two Feb 6 '13 at 21:25
Or possibly look at a factory pattern to dynamically generate your context, complete with the connection string and proper table mappings. –  Forty-Two Feb 6 '13 at 21:27
@Forty-Two passing the table name as a param to the context will work for the first time the context is created, but not subsequent calls. –  tom Feb 6 '13 at 23:02

2 Answers 2

up vote 2 down vote accepted

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

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

share|improve this answer
If you inherit Reporting from Report, it will try to join all Reporting rows to Report rows - not great for this situation even if both tables existed, since that's an extra and unnecessary join. (In this case, it would probably just blow up.) –  zimdanen Feb 6 '13 at 21:32
I may be wrong but my understanding is that this configuration is stored in a static instance within EF –  qujck Feb 6 '13 at 21:34
@zimdanen It's actually a union, not a join, whether not a join is involved depends on the type of hierarchical (table-per-type, etc.). But yeh, like I said I am pretty sure that won't work across databases. Anyhow, I'm not sure what you mean by "unnecessary" since that implies you know of another way he can do it? –  AaronLS Feb 6 '13 at 21:41
Nono, I was agreeing with your comment that the inheritence wouldn't work. I like your conditional ToTable. –  zimdanen Feb 6 '13 at 21:45
@qujck: The fluent api call in this answer would be called every time the context is instantiated, I believe. –  zimdanen Feb 6 '13 at 21:45

Are you able to create the same named view in each database and map to that instead of a variable table name?

I have 2 copies of tables with different names in my solution and deal with that by having 2 contexts and 2 sets of map files (generated text templates)

share|improve this answer
No I cant to that :( –  tom Feb 6 '13 at 21:57
Can you use text templates to generate a slightly different context for each, or create an abstract base context and inherit for each overriding the alternating parts? –  qujck Feb 6 '13 at 22:44

Your Answer


