Als je een index wilt maken en de zoekopdracht wilt versnellen, voeg dan het [Index]-attribuut toe aan het attribuut en genereer automatisch de code als volgt:
We updaten de database en de fout is als volgt:
System.Data.SqlClient.SqlException (0x80131904): table 'dbo. De kolom 'Telefoon' in 'Account' kan niet als sleutelkolom in een index worden gebruikt. at System.Data.SqlClient.SqlConnection.OnError(SqlException-uitzondering, Boolean breakConnection, Action'1 wrapCloseInAction) op System.Data.SqlClient.SqlInternalConnection.OnError(SqlException-uitzondering, Boolean breakConnection, Action'1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) In 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) In System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource'1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean&useCache, Boolean asyncWrite, Booleaan inRetry) In System.Data.SqlClient.SqlCommand.ExecuteNonQuery() In System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher. <NonQuery>b__0(DbCommand t, DbCommandInterceptionContext'1 c) In System.Data.Entity.Infrastructure.Interception.InternalDispatcher'1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func'3 operation, TInterceptionContext onderscheppingContext, Actie'3 uitgevoerd, Actie'3 uitgevoerd) In System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext) In System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery() In System.Data.Entity.Migrations.DbMigrator.ExecuteSql(MigrationStatement migrationStatement, DbConnectionconnection, DbTransaction-transactie, DbInterceptionContext interceptionContext) In System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(MigrationStatement migrationStatement, DbConnectionconnection, DbTransaction-transactie , DbInterceptionContext interceptionContext) In System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable'1 migrationStatements, DbConnection-verbinding, DbTransaction-transactie, DbInterceptionContext InterceptionContext) In System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinTransaction(IEnumerable'1 migrationStatements, DbTransaction-transactie, DbInterceptionContext interceptionContext) In System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinNewTransaction(IEnumerable'1 migrationStatements, DbConnection-verbinding, DbInterceptionContext) interceptionContext) In System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable'1 migrationStatements, DbConnection-verbinding, DbInterceptionContext interceptionContext) bij System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable'1 migrationStatements, DbConnection-verbinding) op System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClass32. <ExecuteStatements>b__30() In System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1. <Execute>b__0() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func'1 operation) In System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation) In System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable'1 migrationStatements, DbTransaction existingTransaction) In System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable'1 migrationStatements) op System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable'1 migrationStatements) In System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, VersionedModel targetModel, IEnumerable'1 operations, IEnumerable'1 systemOperations, Boolean downgrading, Boolean auto) op System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migratie, DbMigration lastMigration) op System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migratie, DbMigration lastMigration) op System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable'1 pendingMigrations, string targetMigrationId, string lastMigrationId) op System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable'1 pendingMigrations, String targetMigrationId, String lastMigrationId) op System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) In System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClasse. <Update>b__d() bij System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) bij System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) op System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) In System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration) op System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() In System.Data.Entity.Migraties.Design.ToolingFacade.BaseRunner.Run()
ClientConnectionId:2b5f614b-caab-4be6-bc1b-a813b7356739 Foutnummer: 1919, Staat:1, Klas: 16 Tafel-dbo. De kolom 'Telefoon' in 'Account' kan niet als sleutelkolom in een index worden gebruikt.
De reden voor de fout is dat alle stringeigenschappen van ef standaard van het type NVARCHAR(Max) zijn,
De oplossing is als volgt:
Voer de lengte van de string uit
Zoals te zien is op de foto, is de creatie geslaagd!
|