views:

255

answers:

1

I'm loosely following an excellent series of blog posts by Kazi Manzur Rashid as a learning exercise for learning how to implement some new (for me at least) design patterns, but I'm getting trouble from the start.

I've basically copied his code for the Database, RepositoryBase and RepositoryBaseTests classes, but when I try to run the tests, I get an error message saying

Unable to create instance of class Booking.Infrastructure.EntityFramework.Repositories.Tests.RepositoryBaseTests. Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0..

Through the debugger I have verified that the exception is thrown on the constructor for the Database class, which looks like this:

public Database(
    IConfigurationManager configurationManager, 
    string connectionstringName)
: base(
    GetConnectionString(configurationManager, connectionstringName), 
    "BookingEntities")
{ // Nothing happens here }

The error is thrown when calling the base constructor, and if I'd hard-code the values that I'm currently sending in, it would look like this:

: base("Dummy connStr", "BookingEntities")

Why doesn't this work?

A: 

"Dummy connStr" is not a valid EF connection string.

A valid EF connection string looks like:

connectionString="metadata=res://*/Data.Model.csdl|res://*/Data.Model.ssdl|res://*/Data.Model.msl;provider=System.Data.SqlClient;provider connection string="Data Source=SERVERNAME\SQLDEV2008;Initial Catalog=DBName;Integrated Security=True;MultipleActiveResultSets=True""
Craig Stuntz
OK. Well, I could figure out that the connection string wouldn't be a valid one, but I assumed it *should* have worked anyway as it did in the example in the article. Anyway, I don't have a database set up yet, so I don't really have a connection string to supply. Is there any (easy) way to build up a dummy string that has all the necessary parts but won't actually do anything?
Tomas Lycken
Just use Northwind.mdb or some other demo DB.
Craig Stuntz
The thing is that I'm doing this to unit test my repository - I don't want to connect to a database at all, just see that the repository gets the correct stuff out of the `Database` object.
Tomas Lycken
The EF won't connect until you do something which requires it, like new up an ObjectContext and select something. Use SQL Server Profiler to prove this.
Craig Stuntz