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

前端 未结 5 2203
挽巷
挽巷 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:02

    The argument of the used base DbContext constructor is called nameOrConnectionString. Hence it supports a name of a connection string from the configuration file, or like in your case an actual connection string.

    The problem with the later is that it doesn't allow specifying the provider name as with the former coming from the configuration, in which case EF uses the one specified in the defaultConnectionFactory configuration element, which in your case is System.Data.Entity.Infrastructure.SqlConnectionFactory, in other words - Sql Server, hence the port not supported exception.

    There are several ways to fix the issue.

    (A) Change the defaultConnectionFactory configuration:

    
    

    (B) Use named configuration connection string and specify explicitly the provider:

    
        
    
    

    and change the constructor to

    public MyDB()
    {
        // ...
    }
    

    or if the name is different than your DbContext derived class name:

    public MyDB() : base(connection_string_name)
    {
        // ...
    }
    

    (C) Use DbConfigurationTypeAttribute:

    [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
    public class MyDB : DbContext
    {
        // ...
    }
    

提交回复
热议问题