Connection Strings for Entity Framework

后端 未结 6 1791
挽巷
挽巷 2020-12-08 04:59

I want to share same Database information across multiple entities in Silverlight.. but I want the connection string to be named xyz and have everyone access that connection

6条回答
  •  温柔的废话
    2020-12-08 05:37

    To enable the same edmx to access multiple databases and database providers and vise versa I use the following technique:

    1) Define a ConnectionManager:

    public static class ConnectionManager
    {
        public static string GetConnectionString(string modelName)
        {
            var resourceAssembly = Assembly.GetCallingAssembly();
    
            var resources = resourceAssembly.GetManifestResourceNames();
    
            if (!resources.Contains(modelName + ".csdl")
                || !resources.Contains(modelName + ".ssdl")
                || !resources.Contains(modelName + ".msl"))
            {
                throw new ApplicationException(
                        "Could not find connection resources required by assembly: "
                        + System.Reflection.Assembly.GetCallingAssembly().FullName);
            }
    
            var provider = System.Configuration.ConfigurationManager.AppSettings.Get(
                            "MyModelUnitOfWorkProvider");
    
            var providerConnectionString = System.Configuration.ConfigurationManager.AppSettings.Get(
                            "MyModelUnitOfWorkConnectionString");
    
            string ssdlText;
    
            using (var ssdlInput = resourceAssembly.GetManifestResourceStream(modelName + ".ssdl"))
            {
                using (var textReader = new StreamReader(ssdlInput))
                {
                    ssdlText = textReader.ReadToEnd();
                }
            }
    
            var token = "Provider=\"";
            var start = ssdlText.IndexOf(token);
            var end = ssdlText.IndexOf('"', start + token.Length);
            var oldProvider = ssdlText.Substring(start, end + 1 - start);
    
            ssdlText = ssdlText.Replace(oldProvider, "Provider=\"" + provider + "\"");
    
            var tempDir = Environment.GetEnvironmentVariable("TEMP") + '\\' + resourceAssembly.GetName().Name;
            Directory.CreateDirectory(tempDir);
    
            var ssdlOutputPath = tempDir + '\\' + Guid.NewGuid() + ".ssdl";
    
            using (var outputFile = new FileStream(ssdlOutputPath, FileMode.Create))
            {
                using (var outputStream = new StreamWriter(outputFile))
                {
                    outputStream.Write(ssdlText);
                }
            }
    
            var eBuilder = new EntityConnectionStringBuilder
            {
                Provider = provider,
    
                Metadata = "res://*/" + modelName + ".csdl"
                            + "|" + ssdlOutputPath
                            + "|res://*/" + modelName + ".msl",
    
                ProviderConnectionString = providerConnectionString
            };
    
            return eBuilder.ToString();
        }
    }
    

    2) Modify the T4 that creates your ObjectContext so that it will use the ConnectionManager:

    public partial class MyModelUnitOfWork : ObjectContext
    {
        public const string ContainerName = "MyModelUnitOfWork";
        public static readonly string ConnectionString
            = ConnectionManager.GetConnectionString("MyModel");
    

    3) Add the following lines to App.Config:

    
    
      
        
      
      
        
        
      
    
    

    The ConnectionManager will replace the ConnectionString and Provider to what ever is in the App.Config.

    You can use the same ConnectionManager for all ObjectContexts (so they all read the same settings from App.Config), or edit the T4 so it creates one ConnectionManager for each (in its own namespace), so that each reads separate settings.

提交回复
热议问题