locked
Using LocalDb For Data-Driven Unit Testing RRS feed

  • Question

  • I am having exactly the same problem described in the thread on this forum entitled

    "Invalid Authorization specification, localdb, VS2012 data driven UT"

    That thread is marked as "answered" but it is not resolved in the OP's case nor in mine. The scenario is simple: I have a localdb that contains test case data and I want this data fed into a data-driven unit test. There is no issue connecting to the database with management studio or via System.Data.SqlClient.SqlConnection. However, when I add specify the connection string via the DataSource attribute of a test method it fails. In more detail:

    If I use the connection string:

    "Provider=System.Data.SqlClient;Data Source=(LocalDb)\v11.0;AttachDbFilename=PathToFile.mdf;Integrated Security=True;Connect Timeout=30";

    I get the error "The System.Data.SqlClient provider is not registered on the local machine"

    If I leave off the Provider parameter, which it apparently doesn't like, I get the error:'

    An OLE DB Provider was not specified in the connection string.

    Using the connection string without the provider specified, however, allows me to open the connection using the System.Data.SqlClient.SqlConnection class.

    This should be a very simple problem to reproduce; just create a test project, a local db and try to use it to feed data-driven unit tests. I have tried every conceivable connection string tweak and I cannot get it to work. 


    Wednesday, September 26, 2012 5:19 PM

Answers

  • None of those resolution steps worked for me nor do I suspect they worked for other people have this issue. What did work was using one of the alternate DataSourceAttribute constructors. In particular:

    DataSourceAttribute(
    string providerInvariantName, 
    string connectionString, 
    string tableName, 
    DataAccessMethod dataAccessMethod
    );

    or, more explicitly:

    [
    DataSource("System.Data.SqlClient", 
    MyConnectionString, 
    "dbo.MyTable", 
    DataAccessMethod.Sequential)
    ]
    
    where
    
    MyConnectionString = "Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\MyDb.mdf;Integrated Security=True;Connect Timeout=30;";

    The constructor that takes a connection string and a table name simply will not work for the scenario I described.


     
    Friday, October 5, 2012 8:49 PM

All replies