Search code examples
entity-framework-4.1metadataconnection-string

Entity Framework - Keyword not supported: 'metadata'


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


Solution

  • 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);
    }