8
respostas

Nao consegui dar update-database

Fui dar o update-database conforme video aula e deu esse erro

Each package is licensed to you by its owner. NuGet is not responsible for, nor does it grant any licenses to, third-party packages. Some packages may include dependencies which are governed by additional licenses. Follow the package source (feed) URL to determine any dependencies.

Package Manager Console Host Version 3.4.4.1321

Type 'get-help NuGet' to see all available NuGet commands.

PM> Add-Migration criaUsuario
To undo this action, use Remove-Migration.
PM> update-database
System.Data.SqlClient.SqlException (0x80131904): An attempt to attach an auto-named database for file C:\Users\Home\Desktop\Entity\lojaComEntily\lojaComEntily\bin\Debug\loja.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at Microsoft.Data.Entity.Storage.RelationalConnection.Open()
   at Microsoft.Data.Entity.Storage.Internal.SqlServerDatabaseCreator.Exists(Boolean retryOnNotExists)
   at Microsoft.Data.Entity.Storage.Internal.SqlServerDatabaseCreator.Exists()
   at Microsoft.Data.Entity.Migrations.HistoryRepository.Exists()
   at Microsoft.Data.Entity.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.Data.Entity.Design.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.Data.Entity.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String contextType)
   at Microsoft.Data.Entity.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_1.<.ctor>b__0()
   at Microsoft.Data.Entity.Design.OperationExecutor.OperationBase.Execute(Action action)
ClientConnectionId:5cfa7e12-2e22-4d5c-8d08-25e9b907993a
Error Number:15350,State:1,Class:14
An attempt to attach an auto-named database for file C:\Users\Home\Desktop\Entity\lojaComEntily\lojaComEntily\bin\Debug\loja.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
PM>
8 respostas

Aparentemente, você já tinha tentado rodar o comando e já tinha criado um arquivo com esse nome.

Faz o seguinte, vai no Executar (ou qualquer pasta) e siga esse caminho C:\Users\Home\Desktop\Entity\lojaComEntily\lojaComEntily\bin\Debug\. Dentro dessa pasta, veja se existe o arquivo loja.mdf. Se sim, exclua ele e roda o comando update-database novamente.

nao tem esse arquivo, eu nao tinha dado esse comando antes

Alguem pode me ajudar por favor

Acesse o seu app.config e verifique a sua connection string. Ela precisa conter o seguinte: User Instance=True;

Se não tiver, adicione lá depois de algum ponto-e-vírgula e teste novamente.

Estou tendo o mesmo problema que o Jhones, e eu também nunca havia executado este comando antes. Também não localizei o arquivo loja.mdf na pasta sugerida...

Então segui o conselho de adicionar o "User Instance=True;" a minha connection string; não deu certo. Passei a receber a seguinte mensagem na execução do comando:

System.Data.SqlClient.SqlException (0x80131904): The user instance login flag is not allowed when connecting to a user instance of SQL Server. The connection will be closed.

E agora, o que eu faço?

Maykon, remova o "User Instance=True" e poste aqui a sua connection string completa, por favor.

estou tendo problema com o update-database o erro é o seguinte

System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.
   at System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue)
   at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey)
   at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules)
   at System.Data.SqlClient.SqlConnectionString..ctor(String connectionString)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions)
   at System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key)
   at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value)
   at System.Data.SqlClient.SqlConnection..ctor(String connectionString, SqlCredential credential)
   at System.Data.SqlClient.SqlConnection..ctor(String connectionString)
   at Microsoft.Data.Entity.Storage.Internal.SqlServerConnection.CreateDbConnection()
   at Microsoft.Data.Entity.Internal.LazyRef`1.get_Value()
   at Microsoft.Data.Entity.Storage.RelationalConnection.get_DbConnection()
   at Microsoft.Data.Entity.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.Data.Entity.Design.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.Data.Entity.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String contextType)
   at Microsoft.Data.Entity.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_1.<.ctor>b__0()
   at Microsoft.Data.Entity.Design.OperationExecutor.OperationBase.Execute(Action action)
Format of the initialization string does not conform to specification starting at index 0.
PM>

Oi Cássio.

No seu caso, acredito que sua string de conexão está errada. Você pode postar ela aqui? Se não sabe onde encontrá-la, vá no arquivo Web.config (pelo Visual Studio mesmo) e copie toda a chave <connection>.