首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >dbml的分部类内的ConnectionString切换

dbml的分部类内的ConnectionString切换
EN

Stack Overflow用户
提问于 2018-02-16 20:24:26
回答 1查看 46关注 0票数 0

关于Point connectionstring in dbml to app.config

如何将分部类中的连接名称替换为参数,以便切换连接字符串?

EN

回答 1

Stack Overflow用户

发布于 2018-02-16 21:31:14

如果您使用的是实体框架,则可以使用此类

代码语言:javascript
复制
public static void ChangeDatabase(
        this DbContext source,
        string initialCatalog = "",
        string dataSource = "",
        string userId = "",
        string password = "",
        bool integratedSecuity = true,
        string configConnectionStringName = "",
        string sqlConnectionString = "")
    /* 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);

            if (string.IsNullOrEmpty(sqlConnectionString))
            {
                // 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;
                sqlConnectionString = sqlCnxStringBuilder.ConnectionString;
            }

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

并使用以下命令调用此类

代码语言:javascript
复制
dbContext = new ORM.CustomyzerEntities();
                this._destDBContext.ChangeDatabase(
                    sqlConnectionString: connectionstringname);

在这里将connectionstringname替换为您的连接字符串

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48826720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档