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

2
  • 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. Commented Sep 11, 2011 at 9: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. Commented Sep 17, 2011 at 3:11

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

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: Generic solution is not always the "correct" one: stackoverflow.com/questions/5625746/… and stackoverflow.com/questions/7110981/…
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.