在运行时更改实体connectionString错误edmx(databasefirst)

时间:2015-05-30 03:08:51

标签: sql wpf entity-framework connection-string

希望也许你可以指出我正确的方向,因为我似乎在一个街区。我想知道如何实现这个扩展类?使用代码在哪里?我使用Prism和我的DbContext在另一个项目中。

修改

我更改了我的代码以反映下面的链接,我对使用情况感到困惑,因为我的代码似乎没有使用新生成的连接字符串。当我输入无效的服务器名称时,我的应用程序仍然能够检索数据。我不应该使用错误的服务器名称来获取数据。

public static class ConnectionTools
{
    // all params are optional
    public static void ChangeDatabase(
        this DbContext source,
        string initialCatalog = "",
        string dataSource = @"",
        string userId = "",
        string password = "",
        bool integratedSecuity = true,
        string configConnectionStringName = "")
    /* this would be used if the
    *  connectionString name varied from 
    *  the base EF class name */
    {
        try
        {
            // use the const name if it's not null, otherwise
            // using the convention of connection string = EF contextname
            // grab the type name and we're done
            var configNameEf = string.IsNullOrEmpty(configConnectionStringName)
                ? source.GetType().Name
                : configConnectionStringName;

            // add a reference to System.Configuration
            var entityCnxStringBuilder = new EntityConnectionStringBuilder
                (System.Configuration.ConfigurationManager
                    .ConnectionStrings[configNameEf].ConnectionString);

            // init the sqlbuilder with the full EF connectionstring cargo
            var sqlCnxStringBuilder = new SqlConnectionStringBuilder
                (entityCnxStringBuilder.ProviderConnectionString);

            // only populate parameters with values if added
            if (!string.IsNullOrEmpty(initialCatalog))
                sqlCnxStringBuilder.InitialCatalog = initialCatalog;
            if (!string.IsNullOrEmpty(dataSource))
                sqlCnxStringBuilder.DataSource = dataSource;
            if (!string.IsNullOrEmpty(userId))
                sqlCnxStringBuilder.UserID = userId;
            if (!string.IsNullOrEmpty(password))
                sqlCnxStringBuilder.Password = password;

            // set the integrated security status
            sqlCnxStringBuilder.IntegratedSecurity = integratedSecuity;

            // now flip the properties that were changed
            source.Database.Connection.ConnectionString
                = sqlCnxStringBuilder.ConnectionString;
        }
        catch (Exception ex)
        {
            // set log item if required
        }
    }
}

用法

    public void LogonClick(object sender, RoutedEventArgs e)
    {
      DialogResult = true;

      // assumes a connectionString name in .config of MyDbEntities
      var ADataEntities = new ADataEntities();
      // so only reference the changed properties
      // using the object parameters by name
      ADataEntities.ChangeDatabase
          (   
              initialCatalog: "",
              userId: "",
              password: "",
              // user Entered DataSource is the only change needed
              dataSource: (DataSource) 
          );

       Close();
     }

在上面的使用代码之后,我验证了我的数据库中的UserName,我的DbContext仍在使用App.config中的原始连接字符串。

  public void Authorize(string userName, string password)
    {

        var AuthContext = new ADataEntities();

        var query = from c in AuthContext.UserNames
                    where (c.UserID == userName && c.EmailAddress == password )
                   select c;
   {

0 个答案:

没有答案