question

HelderLoureno-9287 avatar image
0 Votes"
HelderLoureno-9287 asked HelderLoureno-9287 commented

Problems connecting to one database

Hi,

I'm trying to connect an API database and I have problems, I triead everything. One API connects with no problems and I developed the second one with the first code.
I can connect in SQL server but in code i can't.

The error when I do update-database in Package Manager Console:

 Build started...
 Build succeeded.
 Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot open server "hlourencogmail.onmicrosoft.com" requested by the login.  The login failed.
    at Microsoft.Data.ProviderBase.DbConnectionPool.CheckPoolBlockingPeriod(Exception e)
    at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
    at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
    at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
    at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
    at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
    at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
    at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
    at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)
    at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides)
    at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.OpenDbConnection(Boolean errorsExpected)
    at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternal(Boolean errorsExpected)
    at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
    at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.<>c__DisplayClass18_0.<Exists>b__0(DateTime giveUp)
    at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass12_0`2.<Execute>b__0(DbContext c, TState s)
    at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementation[TState,TResult](Func`3 operation, Func`3 verifySucceeded, TState state)
    at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
    at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation, Func`2 verifySucceeded)
    at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation)
    at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists(Boolean retryOnNotExists)
    at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists()
    at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
    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:10817eeb-fd93-42be-b3ee-c7c533aa1011
 Error Number:40532,State:1,Class:20
 Cannot open server "hlourencogmail.onmicrosoft.com" requested by the login.  The login failed.


The Last connection string, I tried several:

   "ConnectionStrings": {
     "AuthorDbContext": "Server=tcp:database.database.windows.net,1433;Initial Catalog=AuthorsDb;Persist Security Info=False;User ID=username@username.onmicrosoft.com;Password=Password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Authentication=Active Directory Password"
   }


The Startup configure services:

        public void ConfigureServices(IServiceCollection services)
         {
    
       
             services.AddScoped<IAuthorsRepository, AuthorsRepository>();
                  
    
             var mappingConfig = new MapperConfiguration(mc =>
             {
                 //mc.AddProfile(new BookProfile());
             });
    
             IMapper mapper = mappingConfig.CreateMapper();
             services.AddSingleton(mapper);
    
    
             services.AddControllers().AddNewtonsoftJson();
    
    
             services.AddDbContext<AuthorDbContext>(options =>
             options//.UseSqlServer(Configuration.GetConnectionString("BookDbContext")));
             .UseSqlServer(Configuration.GetConnectionString("AuthorDbContext"), options => options.EnableRetryOnFailure()));
    
             services.AddControllersWithViews();
    
      
    
             services.AddSwaggerGen(c =>
             {
                 c.SwaggerDoc("v1", new OpenApiInfo { Title = "AuthorAPI", Version = "v1" });
             });
    
      
         }


I get out of ideas, i'm around this for one week.....please help me.

azure-sql-databasedotnet-aspnet-core-webapidotnet-entity-framework-core
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@HelderLoureno-9287 Are these databases on the same server?
If not please check if you need to configure the firewall to allow your IP address.

0 Votes 0 ·

I copy a connection string from a working API and it doesn't work, the error is the same. I cant figured out what's wrong.

0 Votes 0 ·

0 Answers