Skip to content

How to change a DbSet table name during runtime? #24488

@DefinitelyADev

Description

@DefinitelyADev

Due to business and the need to support a legacy database, which other legacy systems are dependent so it cannot be altered. There is a convention we use.

For example we have a table called Things which has an Id of type varchar(10) and Alias of type varchar(whatever)

Id Alias
ABC1234567 Some Name
ABC7654321 Some Other Name

And those entries each have a corresponding table with the name Thing_{id}
For example:

  • Thing_ABC1234567
  • Thing_ABC7654321

The only thing that works is if I do myContext.MyDbSet.FromSqlRaw("SELECT * FROM myschema.Thing_{id}").
When using myContext.MyDbSet.FromSqlInterpolated($"SELECT * FROM myschema.Thing_{id}") it throws an error

Executing DbCommand [Parameters=[p0='ABC1234567' (Size = 4000), @__p_1='10'], CommandType='Text', CommandTimeout='30']
SELECT `m`.`Id`
FROM (
    SELECT * FROM myschema.Thing_@p0
) AS `m`
LIMIT @__p_1
MySqlConnector.MySqlException (0x80004005): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''ABC1234567'
) AS `m`
LIMIT 10' at line 3
 ---> MySqlConnector.MySqlException (0x80004005): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''ABC1234567'
) AS `m`
LIMIT 10' at line 3
   at MySqlConnector.Core.ServerSession.ReceiveReplyAsyncAwaited(ValueTask`1 task) in /_/src/MySqlConnector/Core/ServerSession.cs:line 816
   at MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) in /_/src/MySqlConnector/Core/ResultSet.cs:line 49
   at MySqlConnector.MySqlDataReader.ActivateResultSet(CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 133
   at MySqlConnector.MySqlDataReader.CreateAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, IDictionary`2 cachedProcedures, IMySqlCommand command, CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 437
   at MySqlConnector.Core.CommandExecutor.ExecuteReaderAsync(IReadOnlyList`1 commands, ICommandPayloadCreator payloadCreator, CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/CommandExecutor.cs:line 60
   at MySqlConnector.MySqlCommand.ExecuteReaderAsync(CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlCommand.cs:line 310
   at MySqlConnector.MySqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlCommand.cs:line 304
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
   at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()

EF Core version: 5.0.2
Database provider: Pomelo.EntityFrameworkCore.MySql
Target framework: .Net Core 3.1
Operating system: Windows 10 Pro
IDE: Visual Studio 2019 16.9.2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions