Skip to content

Support specifying catalog for table mapping #4019

Description

@zpul

I need to access a Table from another database located in the same server, so I did specify its location in the table name but this do not work.

The problem are the angled brackets put in the name of the table. Can I avoid inserting this brackets into the table name?

I receive this exception:

   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader()
   at Microsoft.Data.Entity.Storage.Internal.RelationalCommand.<>c__DisplayClass17_0.<ExecuteReader>b__0(DbCommand cmd, IRelationalConnection con)
   at Microsoft.Data.Entity.Storage.Internal.RelationalCommand.Execute[T](IRelationalConnection connection, Func`3 action, String executeMethod, Boolean openConnection, Boolean closeConnection)
   at Microsoft.Data.Entity.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, Boolean manageConnection)
   at Microsoft.Data.Entity.Query.Internal.QueryingEnumerable.Enumerator.MoveNext()
   at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
   at Microsoft.Data.Entity.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at WebApplication1.Controllers.IdentsController.Details(Nullable`1 id) in D:\git\vnext\WebApplication1\src\WebApplication1\Controllers\IdentsController.cs:line 33

Activity

  1. changed the title [-]Specifying attribute Table="SomeDatabase.dbo.SomeTable" won't work.[/-] [+]Support specifying catalog for table mapping[/+] on Dec 16, 2015
  2. rowanmiller commented on Dec 16, 2015

    @rowanmiller
    Contributor

    Currently we only support schema and table name (and do all the appropriate escaping to deal with extra .s etc.). We agree it would be good to have an overload of ToTable that allows you to specify a catalog though. Moving to the backlog for the moment, since we won't be doing this for initial 7.0.0 RTM.

  3. added
    help wanted
    on Dec 16, 2015
  4. added this to the Backlog milestone on Dec 16, 2015
  5. rigofunc commented on Jan 20, 2017

    @rigofunc

    @rowanmiller Have any plan to release this feature? our team waiting to use this feature in spring of 2017

  6. rowanmiller commented on Jan 20, 2017

    @rowanmiller
    Contributor

    @xyting it's not on our list for an upcoming release. It would make a good external contribution though 😄

  7. nvivo commented on Apr 17, 2017

    @nvivo

    Just to add one use case here, I currently have an application that uses 3 databases databases in the same server, and a dozen servers that access the same 3 databases. Because the connection string is different for each database, each application has 3 connection pools to the same server. This causes lots of connections being open to the same server, which can cause the server to stop accepting connections sometimes.

    This can be kept under control to some extent using Max Pool Size and Connection Lifetime, but at some point I found it simpler to stop using entities in favor of pure SQL for a lot of stuff to be able to reuse idle connections from the pool.

    So, I'm in line for this feature. =)

  8. MaklaCof commented on Jun 17, 2018

    @MaklaCof

    This would also solve problems with common (shared) tables when solving multiple tenants with Separate Database for every tenant:
    each tenant has it's own database, but some tables, like users, groups, countries ..., must be common (shared).

    There is currently no work around.

  9. kryptus47 commented on Nov 9, 2018

    @kryptus47

    Just to add another usecase. I am dealing with a legacy database that is composed of no less than 23 different databases with tons of unmaintainable legacy applications using it and querying across the different databases.

    Basically I am creating an OData Web API using MVC Core + EF Core to abstract the misery for future applications.
    I am also using AutoMapper with EF Core to tranform the objects as needed which is working flawlessely.

    The only show stopper for now is the fact that I cannot specify the catalog and expect entity framework to behave like a good boy.

  10. hnviradiya commented on Feb 28, 2019

    @hnviradiya

    This feature would be great, because single database can't handle large datasets. So many use by separating them for different tenants. in that case some tables are shared. so this feature can be a game changer.

  11. divega commented on Mar 11, 2019

    @divega
    Contributor

    @Mrswain The approach seems ok to me in general. If you send a PR, we can discuss a few details there.

    In the meantime, here are a few things that come to mind:

    1. We usually try to triangulate with other databases besides SQL Server to decide if how first class a concept should be in our relational model and APIs. And I am not sure how database-independent this feature is.

    2. I tend to agree that specifying a non-default catalog is strongly correlated with not wanting that EF Core migrations to try to maintain the database object. ToView implies the latter, so ToView is a good fit, and ToTable, not so much. FWIW, in 3.0 we are making query types just "entities with no keys" and whether you call ToTable or ToView becomes independent of whether the table has a key.

    3. I may be wrong, but I don't think we want/need the ability to set a default catalog, at least for the scenario in which most tables are mapped to the database on which we opened the connection and only a few of them live somewhere else.

  12. 8 remaining items

  13. removed
    help wanted
    on Aug 5, 2019
  14. removed
    good first issue
    on Oct 17, 2019
  15. John0King commented on Jul 30, 2021

    @John0King

    Should there is a Option for DbContext to just inlucde DatabaseName in the Query ?
    I mean for different Database , EF Core should use different DbContext, and with an option to specify the DatabaseName for the DbContext

    [DatabaseName("Db1")]
    public class Db1DbContext :DbContext{}
    
    
    //or
    
    public class Db2DbContext:DbContext
    {
       override onModelCreate(ModelBuilder builder)
    {
        builder.UseDatabaseName("Db2")
    }
    } 
  16. alexreich commented on Aug 16, 2021

    @alexreich

    Another workaround (similar to @ajcvickers answer above and derived from this blog) - this removes hardcoding of database names:

    using Microsoft.EntityFrameworkCore;  
    using Microsoft.EntityFrameworkCore.Metadata.Builders;  
      
    namespace MulitpleDb.Sample.Data  
    {  
        public class FooConfiguration : IEntityTypeConfiguration<FooTableClass>  
        {  
            public void Configure(EntityTypeBuilder<Planet> builder)  
            {  
                builder.ToTable("FooTable","[Database2].[dbo]");
                builder.HasKey(e => e.Id);  
                builder.Property(a => a.Name).IsRequired();  
            }  
        }  
    }  

    Interceptor Workaround classes:

        public class GlobalCommandInterceptor : IObserver<KeyValuePair<string, object>>
        {
            public void OnCompleted()
            {
            }
    
            public void OnError(Exception error)
            {
            }
    
            public void OnNext(KeyValuePair<string, object> value)
            {
                if (value.Key == RelationalEventId.CommandExecuting.Name)
                {
                    var command = ((CommandEventData)value.Value).Command;
                    command.CommandText = command.CommandText.Replace(
                        "[[",
                        "[").Replace("]]]", "]").Replace("]]", "]");
                }
            }
        }
        public class GlobalListener : IObserver<DiagnosticListener>
        {
            private readonly GlobalCommandInterceptor _commandInterceptor = new GlobalCommandInterceptor();
            public void OnCompleted()
            {
            }
    
            public void OnError(Exception error)
            {
            }
    
            public void OnNext(DiagnosticListener value)
            {
                if (value.Name == DbLoggerCategory.Name)
                    value.Subscribe(_commandInterceptor);
            }
        }

    and called in Startup.cs:

    DiagnosticListener.AllListeners.Subscribe(new GlobalListener());
  17. John0King commented on Aug 17, 2021

    @John0King

    From 2015-12 to 2021-8 , almost 6 years , anything new on this (expect drop from next version again)

  18. roji commented on Aug 17, 2021

    @roji
    Member

    @John0King see this for how we decide what to work on. This issue has received only 5 votes since it was opened in 2015, and there is are reasonable workaround (i.e. introduce the database name directly into the SQL via a command interceptor).

  19. tbasallo commented on Apr 10, 2024

    @tbasallo

    The workarounds work. However, it seems a little heavy handed. I now have to listen to, check and potentially change every query. I am not sure of the overall impact since I just tested this and it works, but, jeez. I am a little concerned.

    I realize that multiple databases are perhaps not very common (maybe??), I've moved our team into Azure SQL and PAAS which doesn't support cross-database queries (to get away from maintenance and administration) and I now use schemas for what we used to use databases for. I just happened to find myself in someone else's org doing work on servers that have upwards of 30 databases each and I wanted to use EF core and not start hand-writing SQL again...

    Food for thought, I work in a very large organization where every non-PAAS SQL database uses multiple databases. It's a culture thing in this enterprise (the Oracle databases we have do the same). They would be hard-pressed to move to EF for this alone. And they would not bother coming in here and voting for this to ensure the team sees it as important. I think most people are this way. While I know you need some way to determine what's important, somethings should just work if they work in the underlying systems (like cross-database queries).

  20. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions