我已经修改了web.config连接字符串。但是,在调试期间,我仍然看到旧的连接字符串。
因此,我已注释掉(并删除了)旧的连接字符串,但是通过服务器资源管理器添加了新的连接资源。在测试通过向导在服务器左侧面板Explorer的它说,连接上的连接。
遵循此向导后,当我访问web.config时,看不到新的连接字符串。
问题:我怀疑,我没有按照添加连接字符串的步骤进行操作-如何从设计器添加或更新连接字符串(在设计器属性面板中,编辑显示为灰色,输出类型构建为装配体,然后正确仅单击即可为我提供添加实体等选项,删除字符串并运行应用程序,不会提示您输入连接字符串向导)
下面是字符串-
<connectionStrings><add name="MaintRecordsDB_v1" connectionString="metadata=res://*/Models.DB.Model.csdl|res://*/Models.DB.Model.ssdl|res://*/Models.DB.Model.msl;provider=System.Data.SqlClient;provider connection string="data source=xxx.sample.net;initial catalog=MainDB;user id=maintRoot;password=hidden;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" /><add name="MainDBentities" connectionString="metadata=res://*/Models.DB.Model.csdl|res://*/Models.DB.Model.ssdl|res://*/Models.DB.Model.msl;provider=System.Data.SqlClient;provider connection string="data source=windflower.arvixe.com;initial catalog=MX_Dashboard;user id=maintRoot;password=hidden;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings>
编辑问题2:如何添加另一个EF连接字符串,例如MaintDB2
使用设计器,以及在哪里手动更新。
假设您的DbContext
类不知道上下文类的样子,如果类已经生成并假设它是局部类,则可以尝试使用以命名连接字符串作为参数的构造函数向其添加另一个局部类部分。
首先将命名连接添加到您的app.config / web.config中:
<connectionStrings>
...
<add name="MyOtherConnection" connectionString="metadata=res://*/blahblahblah;provider=System.Data.SqlClient;provider connection string="Data Source=ABunchOfOtherStuff;"
providerName="System.Data.EntityClient" />
</connectionStrings>
然后在另一个(未生成的)文件中使用构造函数将匹配的局部类添加为连接字符串名称:
// the class name must match the name of your existing context
public partial class MyContext : DbContext
{
public MyContext(string connectionStringName) : base("name=" + connectionStringName)
{
}
}
然后通过传递连接字符串的名称来使用上下文,这由一些无用的代码演示:
// ...
using (var context = new MyContext("MyOtherConnection"))
{
var id = 1;
var collection = context.MyEntities.Where(a => a.ID == id).ToList();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句