Unable to use the existing database in unit tests with the Effort framework

advertisements

I am trying to write test using a database, hosted in Azure SQL, with Effort framework on Entity Framework 6.

When executing the following code, an exception is thrown:

[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
    EffortProviderConfiguration.RegisterProvider();
}

[TestMethod]
public void TestMethod1()
{
    const string connectionString = "Data Source=***;Initial Catalog=my_catalog;User ID=user;Password=password;provider=System.Data.SqlClient";
    IDataLoader loader = new EntityDataLoader(connectionString);
    using (var ctx = new UsersDbContext(Effort.DbConnectionFactory.CreatePersistent("cool", loader)))
    {
        var usersCount = ctx.Users.Count();
    }
}

Exception thrown in Count() execution:

Effort.Exceptions.EffortException: Unhandled exception while trying to initialize the content of 'Table' table ---> System.ArgumentException: Keyword not supported: 'data source'.

The same exception is thrown when replacing EffortProviderConfiguration.RegisterProvider() with app.config settings.

When using exactly the same connection string for creation of the UsersDbContext it succeeds and the data is accessible. In addition, creating context with Effort persistent or transient mode, without connection string, works well too.

What should be done to initialize a connection with existing data from a real DB?


You are specifying connection string in wrong format. You are using ADO.NET/Linq2SQL connection string format, whenever EntityDataLoader requires connection string that will fit EntityConnection (class from EntityFramework). You can read about connection strings for EF here: http://msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnection.connectionstring(v=vs.110).aspx

Saying it short, your connection string should look like:

"Provider=System.Data.SqlClient;
 Metadata=c:\metadata|c:\Metadata\Sql;
 Provider Connection String='Data Source=localhost; Initial Catalog=AdventureWorks;Integrated Security=True;Connection Timeout=60' "

Right now in your code you are specifying only Provider Connection String part.