How to set manually an Oracle Connection String in a DbContext

后端 未结 3 2007
南旧
南旧 2021-02-15 23:51

I have the following connection string:



        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-16 00:33

    Just found the solution after struggling with this all afternoon. Seems that the constructor or DbContext will use either the connection string or connection string name, which is not the same, if you pass a connection string it will default to SqlClient which is the only thing bundled by default in .NET

    Now, if you, instead of using the whole connection string, pass just the connection string name then it will internally parse also the "providerName" parameter which has the assembly name for the DB provider Oracle.DataAccess.Client for instance.

    So instead of passing the connection string to the DbContext constructor just pass the connection string name, like this:

    .config file:

    
        
        
    
    
        
    
    

    And in your DbContext:

    public MyDbContext()
        : base("DefaultConnection")
    {
    
    }
    

    That way you just set up a config key with the name of the connection string you want to hook the context to and use it in the constructor. If you do it this way EF will automatically parse the whole connection string tag and not only the connectionString attribute value, hence, load the right provider.

    Do note that I'm using Oracle.ManagedDataAccess.Client which is newer and only included in ODAC / ODP.NET v12 and above. If you use ODAC 11 you should use Oracle.DataAccess.Client in providerName instead.

提交回复
热议问题