locked
Error: Provider Name for SqlConnection not found in EF 4.1 RC RRS feed

  • Question

  • Hi,

    we are using EF 4.1 RC with Code First. We are providing the connection to the Database manually via a existing SqlConnection. In EF 4.1 CTP 5 everything worked fine. Since EF 4.1 RC we get a NotSupportedException with the message "Unable to determine the provider name for connection of type 'System.Data.SqlClient.SqlConnection'." when our application is starting to work with our derived DbContext class.

    What is the problem are what are we doing wrong?

    Thanks

    Daniel


    Friday, March 25, 2011 8:21 AM

Answers

  • Hello Daniel,

     

    Sorry for late! From the exception message and your code, we can see that your code doesn't have a defination of the provider. If you have any time, could you please try to do like this:

    public class CodeFirstContext: DbContext
    {
     public CodeFirstContext() :base("connection")
     {
     }
    }
    
    Then add a app.config or web.config file to your project. In the configration file, add code like this:
    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>
      <connectionStrings>
        <add
          name="connection"
          providerName="System.Data.SqlClient"
          connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=SSPI"/>
      </connectionStrings>
      </configuration>
    Then try to run!
    I hope this will be useful!
    Good day,

    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jackie-Sun Monday, May 21, 2012 9:07 AM
    Tuesday, April 5, 2011 9:06 AM

All replies

  • Hello Daniel,

     

    Welcome to the EF Forum!

    According to your description, could you please show us some code of your DbContext class and the configration in your config file? Before that I think you could try to change the  connection method.

    You know,  the connection string should be set using one of the constructors of DbContext.  For the example you posted if the connection string is in the app.config/web.config then probably the easiest approach is to pass the name of your connection string to the DbContext constructor.  For example:

     

        public class CodeFirstContext : DbContext

        {

            public CodeFirstContext()

                : base("EFConnectionString")

            {

            }

        }

     

    DbContext will then look for a connection string with the given name in the config file.

     

    You can also use the “name=” format like so:

     

        public class CodeFirstContext : DbContext

        {

            public CodeFirstContext()

                : base("name=EFConnectionString")

            {

            }

        }

     

    This will work the same as the first example except that if no connection string with the given name is found in the config file then an exception is thrown. ( In the first example, the string would be treated as a database name and the connection would be created by convention.)

     

    You could also pass the full connection string to the context constructor—for example:

     

        public class CodeFirstContext: DbContext

        {

            public CodeFirstContext()

                : base(ConfigurationManager.ConnectionStrings["EFConnectionString"])

            {

            }

        }

     

    I hope this can help you.

     

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Jackie-Sun Wednesday, March 30, 2011 6:41 AM
    • Unproposed as answer by Slyer85 Wednesday, March 30, 2011 11:50 AM
    Monday, March 28, 2011 7:13 AM
  • Hello Jackie,

    what we want to do is the following:

    Our context looks like the following snippet:

    public class CodeFirstContext: DbContext
    {
     public CodeFirstContext(SqlConnection connection)
     :base(connection, false)
     {
     }
    }
    

    We use it in that way:

    SqlConnection sqlConnection = new SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=SSPI");
    
    new CodeFirstContext(sqlConnection);
    
    

    And if we do this, we get the described NotSupportedException with the Message "Unable to determine the provider name for connection of type 'System.Data.SqlClient.SqlConnection'."

    Regards,

    Daniel


    Wednesday, March 30, 2011 12:11 PM
  • Hello Daniel,

     

    Sorry for late! From the exception message and your code, we can see that your code doesn't have a defination of the provider. If you have any time, could you please try to do like this:

    public class CodeFirstContext: DbContext
    {
     public CodeFirstContext() :base("connection")
     {
     }
    }
    
    Then add a app.config or web.config file to your project. In the configration file, add code like this:
    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>
      <connectionStrings>
        <add
          name="connection"
          providerName="System.Data.SqlClient"
          connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=SSPI"/>
      </connectionStrings>
      </configuration>
    Then try to run!
    I hope this will be useful!
    Good day,

    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jackie-Sun Monday, May 21, 2012 9:07 AM
    Tuesday, April 5, 2011 9:06 AM
  • Hi Daniel,

    You need to mention providerName as "System.Data.SqlClient" for the connection string..

    have a nice day..


    Regards, Harikrishna Y

    Friday, May 18, 2012 11:33 AM