I am using C#, Visual Studio 2010 and Entity Framework 4. I have an assembly that contains multiple entity models. Project requirements are such that I am not storing any connection information in the app.config.
I have written a method that returns an entity connection string when I supply the name of the model I wish to load.
public static string GetEntityConnectionString(string modelName)
{
const string providerName = "somedatabaseprovider";
string metadata = string.Format(@"res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", modelName);
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder
{
Provider = providerName,
ProviderConnectionString = GetProviderConnectionString(),
Metadata = metadata
};
return entityBuilder.ToString();
}
I now want to make it a little more bullet-proof by passing the entity model type, instead of a literal string for the model name. When I am editing the entity model in Visual Studio, the Properties window for MyModel (of type ConceptualEntityModel) contains a property called ‘Entity Container Name’ that shows MyEntities, and another property called ‘Namespace’ which shows MyModel.
At design-time, the type that I have access to is MyEntities. How can I derive the value stored in the ‘Namespace’ property of the ConceptualEntityModel at run-time?
Well, if your goal is to make this “bulletproof,” this won’t work. The string you call
modelNameis not actually the model name, but the resource name. In your case it coincidently happens to be the same as the model name, but that isn’t always true.