1

I have the following class:

public class EFRepository<TContext> : IDisposable where TContext : DbContext, IObjectContextAdapter, new()
{
    private TContext context;

    public EFRepository(string connectionStringName)
    {
        context = new TContext();
        context.Database.Connection.ConnectionString =
            ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
    }
}

with the following connection string:

<connectionStrings>
    <add name="EntitiesConnection" connectionString="metadata=res://*/EntityModel.csdl|res://*/EntityModel.ssdl|res://*/EntityModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=Bob-PC;initial catalog=Entities;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

  </connectionStrings>

Being called like this:

var Entities = new EFRepository<EntitiesConnection>("EntitiesConnection");

Which throws the error in the subject line. I've seen the solutions using the EntityStringBuilder, however the Connection property is read only. Any ideas on how to make this work?

Thanks,

Bob

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
Beaker
  • 2,804
  • 5
  • 33
  • 54
  • Your solution cannot work. You are passing EF connection string to the class expecting SQL connection string. Once you are using EDMX and metadata in connection string you must use context's constructor to pass connection string. – Ladislav Mrnka Sep 11 '11 at 09:10
  • @Ladislav - I had a suspicion this was not doable, but not sure if that was the case or not. Thank you for pointing out this fundamental issue. – Beaker Sep 17 '11 at 03:11

1 Answers1

3

DbContext already has a constructor that accepts a connection string or name. Can you modify your existing context classes to include a constructor that accepts a connection string parameter and call base(connectionStringOrName)?

So a context would look something like:

public class SomeContext : DbContext, IObjectContextAdapter
{
    public SomeContext(string connectionStringOrName)
        : base (connectionStringOrName)
    {
    }

    // Rest of the implementation...
}

And then the constructor of EFRepository<TContext> would look like:

public EFRepository(string connectionStringName)
{
    context = new TContext(connectionStringName);
}
John Allers
  • 3,052
  • 2
  • 30
  • 36
  • This does work, however it is no longer a generic repository which is the reason I used this pattern in the first place. If no one else comes up with a generic solution, I'll mark this as the answer since it does work. – Beaker Sep 11 '11 at 02:44
  • @Beaker: Generic solution is not always the "correct" one: http://stackoverflow.com/questions/5625746/generic-repository-with-ef-4-1-what-is-the-point/5626884#5626884 and http://stackoverflow.com/questions/7110981/the-repository-itself-is-not-usually-tested – Ladislav Mrnka Sep 11 '11 at 09:20
  • @Ladislav - Thank you, I do realize it is important not to always assume that using generics leads to a better solution. However, in this case I feel like there is benefit, but simply can't make them work. – Beaker Sep 17 '11 at 03:10