Se você quiser criar um índice e acelerar a consulta, adicione o atributo [Index] ao atributo e gere automaticamente o código da seguinte forma:
Atualizamos o banco de dados e o erro é o seguinte:
System.Data.SqlClient.SqlException (0x80131904): tabela 'dbo. A coluna 'Telefone' em 'Conta' não pode ser usada como uma coluna chave em um índice. 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) Em System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite) Em System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource'1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) Em System.Data.SqlClient.SqlCommand.ExecuteNonQuery() Em System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher. <NonQuery>b__0(DbCommand t, DbCommandInterceptionContext'1 c) Em System.Data.Entity.Infrastructure.Interception.InternalDispatcher'1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget alvo, operação Func'3, TInterceptionContext interceptionContext, Ação'3 executando, Ação'3 executada) Em System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(Comando DbCommand, DbCommandInterceptionContexto interceptionContext) Em System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery() Em System.Data.Entity.Migrations.DbMigrator.ExecuteSQL (MigrationStatement migrationStatement, conexão DbConnection, transação DbTransaction, DbInterceptionContext interceptaçãoContexto) Em System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(MigrationStatement migrationStatement, conexão DbConnection, transação DbTransaction , DbInterceptionContexto interceptaçãoContexto) Em System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable'1 migrationStatements, conexão DbConnection, transação DbTransaction, DbInterceptionContexto interceptaçãoContexto) Em System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinTransaction(IEnumerable'1 migrationStatements, DbTransaction transaction, DbInterceptionContext interceptaçãoContexto) Em System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinNewTransaction(IEnumerable'1 migrationStatements, conexão DbConnection, DbInterceptionContext interceptaçãoContexto) Em System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable'1 migrationStatements, conexão DbConnection, DbInterceptionContexto interceptionContexto) em System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable'1 migrationStatements, conexão DbConnection) em System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClass32. <ExecuteStatements>b__30() Em System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1. <Execute>b__0() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func'1 operation) Em System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation) Em System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable'1 migrationStatements, DbTransaction existentingTransaction) Em System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable'1 migrationStatements) em System.Data.Entidade.Migrações.Infraestrutura.MigratorBase.ExecuteStatements(IEnumerable'1 migrationStatements) Em System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, VersionedModel targetModel, IEnumerable'1 operations, IEnumerable'1 systemOperations, Boolean rebaixando, auto booleano) em System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration) em System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration) at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable'1 pendentingMigrations, StringTargetMigrationId, String lastMigrationId) em System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable'1 pendentingMigrations, StringTargetMigrationId, String lastMigrationId) em System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) Em System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClasse. <Update>b__d() em System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Ação deveSucedeRDeixarBancoDados) em System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Ação deveSucederParaDeixarBanco) em System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) Em System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration) em System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() Em System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
ClientConnectionId:2b5f614b-caab-4be6-bc1b-a813b7356739 Número de erro: 1919, Estado: 1, Turma: 16 Mesa 'dbo. A coluna 'Telefone' em 'Conta' não pode ser usada como uma coluna chave em um índice.
A razão do erro é que todas as propriedades da string do ef são do tipo NVARCHAR(Max) por padrão,
A solução é a seguinte:
Execute o comprimento da corda
Como mostrado na foto, a criação foi bem-sucedida!
|