2015-09-27 65 views
0

好吧,而不是與xml戰鬥,我被引導到EntityConnectionStringBuilder。所以這是我迄今爲止所擁有的。 EnityManagerFactory調用創建方法。現在Create方法創建並返回一個EntityManager,在這種情況下稱爲UnionEntities使用EntityConnectionStringBuilder建立一個動態的IdeaBlade連接字符串

public UnionEntities Create() 
     { 
      string providerName = "System.Data.SqlClient"; 
      string serverName = ConfigurationManager.AppSettings["DatabaseServer"]; 
      string databaseName = ConfigurationManager.AppSettings["DatabaseName"]; 
      string userName = ConfigurationManager.AppSettings["DatabaseUserName"]; 
      string password = ConfigurationManager.AppSettings["DatabasePassword"]; 
      bool integratedSecurity = bool.Parse(ConfigurationManager.AppSettings["IntegratedSecurity"]); 

      SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder(); 

      // Set the properties for the data source. 
      sqlBuilder.DataSource = serverName; 
      sqlBuilder.InitialCatalog = databaseName; 
      sqlBuilder.IntegratedSecurity = integratedSecurity; 
      sqlBuilder.UserID = userName; 
      sqlBuilder.Password = password; 

      // Build the SqlConnection connection string. 
      string providerString = sqlBuilder.ToString(); 

      // Initialize the EntityConnectionStringBuilder. 
      EntityConnectionStringBuilder entityBuilder = 
       new EntityConnectionStringBuilder(); 

      //Set the provider name. 
      entityBuilder.Provider = providerName; 

      // Set the provider-specific connection string. 
      entityBuilder.ProviderConnectionString = providerString; 

      // Set the Metadata location. 
      entityBuilder.Metadata = @"res://*/UnionModel.csdl| 
         res://*/UnionModel.ssdl| 
         res://*/UnionModel.msl"; 

      var manager = new UnionEntities(); 
      manager.Saving += SaveValidationHelper.SavingHandler; 
      manager.DefaultQueryStrategy = QueryStrategy.DataSourceOnly; 

      return manager; 
     } 

現在常新UnionEntities()使用在app.Config fileConnectionString。我現在如何告訴它使用我的EntityConnectionStringBuilder對象?

回答

1

您可以在DevForce中實現IDataSourceKeyResolver以動態構建或修改連接字符串。鏈接將帶您到DevForce資源中心的一個主題,但如果您在那裏搜索「IDataSourceKeyResolver」,您可以找到其他信息和示例。