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="data source=Bob-PC;initial catalog=Entities;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" 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
DbContextalready 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 callbase(connectionStringOrName)?So a context would look something like:
And then the constructor of
EFRepository<TContext>would look like: