尝试在运行时使用连接字符串或从App.config中删除它。但是我收到错误。
fyi: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();
}
}
错误
类型' System.ArgumentException'的例外情况发生在 System.Data.dll但未在用户代码中处理
其他信息:不支持关键字:'初始目录'。
任何其他解决方案?
答案 0 :(得分:0)
参考link。有效的连接字符串应如下所示
<add name="XXXX"
connectionString="Data Source=MONTGOMERY-XXXX;Initial Catalog=XXXX;Integrated Security=True;"
providerName="System.Data.SqlClient" />
我认为你错过了提供商名称。尝试添加并检查
答案 1 :(得分:0)
问题解决了!设置默认连接工厂!
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=.; Integrated Security=True; MultipleActiveResultSets=True" />
</parameters>
</defaultConnectionFactory>
使用以下DbContext。
public class Model : DbContext
{
public Model()
: base(ConnectionString())
{
}
public virtual DbSet<MyEntity> MyEntities { get; set; }
private static string ConnectionString()
{
// logic here
return "Data Source=(local);Initial Catalog=xx;Integrated Security=False;User ID=sa;Password=xx;MultipleActiveResultSets=True;App=EntityFramework";
}
}