I created EDMX using EF 5 with the Model First approach, i.e. I started with an empty designer and modeled my objects. Now I want to be able to use this model defined in EDMX, but supply SQL Server runtime strings without modifying the configuration file.
I know how to pass the connection string to DbContext, but the problem is finding metadata for the mappings within the assembly.
For example, my EDMX has this connection string in app.config
<add name="MesSystemEntities" connectionString="metadata=res://*/Data.DataContext.EntityFramework.MesSystem.csdl|res://*/Data.DataContext.EntityFramework.MesSystem.ssdl|res://*/Data.DataContext.EntityFramework.MesSystem.msl;provider=System.Data.SqlClient;provider connection string="data source=MyMachine;initial catalog=MesSystem;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
The part I'm missing is "metadata=res://*/Data.DataContext.EntityFramework.MesSystem.csdl|res://*/Data.DataContext.EntityFramework.MesSystem.ssdl|res://*/Data.DataContext.EntityFramework.MesSystem.msl;"
I want to create a DbContext programmatic transfer in the SQL Server connection string, but "add" part of the metadata.
This is what I would like to create using a T4 file ...
public partial class MesSystemEntities : DbContext { public MesSystemEntities() : base("name=MesSystemEntities") { } public MesSystemEntities(string sqlServerConnectionString) : base(GetEfConnectionString(sqlServerConnectionString)) { } private string GetEfConnectionString(string sqlServerConnectionString) {
My question is how to get the metadata that I need in the T4 generation file to create an Entity Framework connection without hard coding for each EDMX file
OR
Is there an easier way to programmatically load metadata from an assembly?
Jim
source share