这是我的 postgresql 迁移代码。在 visual studio 中运行项目时没有问题。
services.AddDbContext<myDbContext>(options => options.UseNpgsql("User ID=myuser;Password=123;Host=localhost;Port=5432;Database=testDb;Pooling=true;"));
using var scope = services.BuildServiceProvider().CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<myDbContext>();
dbContext.Database.Migrate();
但是当我在 docker compose 模式下运行时,我在 dbContext.Database.Migrate()
行收到此错误
ExtendedSocketException: Cannot assign requested address [::1]:5432 This exception was originally thrown at this call stack: System.Net.Sockets.Socket.DoConnect(System.Net.EndPoint, System.Net.Internals.SocketAddress) System.Net.Sockets.Socket.Connect(System.Net.EndPoint) Npgsql.NpgsqlConnector.Connect(Npgsql.Util.NpgsqlTimeout)
最佳答案
你能试试这个连接字符串吗
"User ID=myuser;Password=123;Host=host.docker.internal;Port=5432;Database=testDb;Pooling=true;"
我的意思是,使用 host.docker.internal
而不是 localhost
关于postgresql - docker compose postgresql 连接 ExtendedSocketException : Cannot assign requested address,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69005743/