【问题标题】:.net core - database url parser.net core - 数据库 url 解析器
【发布时间】:2018-02-05 01:38:18
【问题描述】:

我正在 Heroku 上部署我的 .net 核心应用程序。 Heroku 提供以下格式的数据库 url:

[database type]://[username]:[password]@[host]:[port]/[database name]

EF 期望连接字符串的格式为:

host=[host];user id=[username];password=[password];database=[database name];pooling=true;

是否有任何解析器可用于转换,或者我必须自己编写相同的逻辑。

【问题讨论】:

    标签: c# postgresql entity-framework heroku


    【解决方案1】:

    简单的 url 解析工作:

    Uri url;
    bool isUrl = Uri.TryCreate("postgres://1user:1password@dbserver.com:4568/testdb", UriKind.Absolute, out url);
    if(isUrl) {
        Console.WriteLine("Host: "+url.Host);
        Console.WriteLine("Port: "+ url.Port);
        Console.WriteLine("Database: "+ url.LocalPath.Substring(1));
        Console.WriteLine("Username: "+ url.UserInfo.Split(':')[0]);
        Console.WriteLine("Password: "+ url.UserInfo.Split(':')[1]);
        var connectionUrl = $"host={url.Host};username={url.UserInfo.Split(':')[0]};password={url.UserInfo.Split(':')[1]};database={url.LocalPath.Substring(1)};pooling=true;";
        Console.WriteLine(connectionUrl);
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    • 2018-09-07
    相关资源
    最近更新 更多