C# Entity Framework: Keyword not supported: 'port'

前端 未结 5 2207
挽巷
挽巷 2020-12-20 16:38

Hello I have more than one project connecting to a certain DB that is CodeFirst Entity Framework.

All Projects are able to connect successfully except for one stubbo

5条回答
  •  不思量自难忘°
    2020-12-20 17:10

    The error similar to the one listed above comes while working with ASP.net core and Database. The default database Provider with the ASP.net core is the SQL Server but, in case you are using a different provider for example, PostgreSQL and didn't correctly write or configure the DBContext code in the startup.cs

    For example - Following code is written with an intent to connect to PostgresSQL then it will result in error ArgumentException: Keyword not supported: 'port'.

    public void ConfigureServices(IServiceCollection services)
    {
       services.AddMvc();
       var connection = @"Server=localhost;Port=5432;Database=NewDB;User Id=xxxxx;Password=cr@aaaa;";
       services.AddDbContext(options => options.UseSqlServer(connection));
    
        // ...
    
    }
    

    And the reasons is user is trying to connect to PostgreSQL but did change the default Option UseSQLServer after configuring the PostgreSQL string.

    To fix the issue change the option


    options.UseSqlServer(connection)) -> options.UseNpgsql(connection))


提交回复
热议问题