I have a situation where I need to create a new table in a separate schema through EntityFramework that has the same table name in a different schema.
How can this be done through EntityFramework? I am currently locked to EntityFramework 5.0.4, so I cannot move on to EntityFramework 6.
Existing table:
CREATE TABLE dbo.TableA ( /*table contents*/ );
New table:
CREATE TABLE AnotherSchema.TableA ( /*table contents*/ );
I believe I have already created the POCO classes correctly, and MigrationBuilder's Up()
and Down()
methods explicitly use the schema names:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.EnsureSchema(name: "AnotherSchema");
migrationBuilder.CreateTable(schema: "AnotherSchema", name: "TableA", ...);
// ...
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(schema: "AnotherSchema", name: "TableA");
}
However, when I execute Update-Database
I get the following error:
PM> Update-Database
Build started...
Build succeeded.
Applying migration 'xxxxxxxxxx'.
Failed executing DbCommand (55ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [AnotherSchema].[TableA] (
/*table contents*/
);
Microsoft.Data.SqlClient.SqlException (0x80131904): There is already an object named 'TableA' in the database.
at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at Microsoft.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean isAsync, Int32 timeout, Boolean asyncWrite)
at Microsoft.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String methodName)
at Microsoft.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
ClientConnectionId:xxxxxxxx
Error Number:2714,State:6,Class:16
There is already an object named 'TableA' in the database.
PM>
User contributions licensed under CC BY-SA 3.0