c# - 在运行时设置连接字符串 - EF Code First

标签 c# sql-server entity-framework

尝试在运行时使用连接字符串或将其从 App.config 中删除。但是我收到错误。

仅供引用:EF 6 CodeFirst

public class DfDbContext : DbContext
{
    public DfDbContext()
        : base(GetConnectionString())
    {

    }


    private static string GetConnectionString()
    {
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
        builder.DataSource = "xxxxx";
        builder.InitialCatalog = "xxxx";
        builder.UserID = "xxx";
        builder.Password = "xxxx";
        builder.MultipleActiveResultSets = true;
        builder.PersistSecurityInfo = true;
        return builder.ConnectionString.ToString();

    }
}

错误

An exception of type 'System.ArgumentException' occurred in System.Data.dll but was not handled in user code

Additional information: Keyword not supported: 'initial catalog'.

任何替代解决方案?

最佳答案

引用自link .一个有效的连接字符串应该如下所示

<add name="XXXX" 
     connectionString="Data Source=MONTGOMERY-XXXX;Initial Catalog=XXXX;Integrated Security=True;" 
     providerName="System.Data.SqlClient" />

我认为您漏掉了提供商名称。尝试添加它并检查

关于c# - 在运行时设置连接字符串 - EF Code First,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36325895/

相关文章:

c# - dotnet 返回 2 种类型中的 1 种

c# - 如何将计算值添加到此 TryUpdateModelAsync<>

c# - XAML TextBlock 以编程方式设置特殊字符?

c# - 是否有任何实用程序可以动态生成在运行时实现接口(interface)的对象?

sql-server - 按聚集索引顺序选择数据而不使用 ORDER BY

sql - 如何在sql server中将119.305.1983984.1转换为1.1983984.305.119?

sql-server - 使用 SQL Server 进行基于 PKI 的 x.509 证书身份验证?

c# - EF 添加迁移抛出 System.OutOfMemoryException

c# - 动态对象,可持久保存到 Azure 并可通过 Dynamic Linq 进行查询

c# - 如何使用 NSubstitute 模拟 DbContext 然后添加/删除数据