我修改了web.config连接字符串。但是,在调试期间,我仍然看到旧的连接字符串。
因此,我已经注释(并删除)了旧的连接字符串,但是,并通过服务器资源管理器添加了一个新的连接资源。在测试连接时,通过服务器资源管理器中左侧面板上的向导-它说已连接。
在遵循这个向导之后,当我访问web.config时,我看不到新的连接字符串。
问题:我怀疑我没有遵循添加连接字符串的步骤--如何从designer添加或更新连接字符串(在designer属性面板中,编辑已变灰,输出类型已生成到程序集,右键单击仅给我添加实体等选项,删除字符串并运行应用程序,不会提示连接字符串向导)
下面是绳子-
<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:如何为例如使用设计器的MaintDB2添加另一个EF连接字符串,以及在哪里手动更新。
发布于 2014-03-02 07:28:38
在不知道上下文类是什么样子的情况下,比如说您的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();
}发布于 2014-03-02 09:35:42
在MVC中,有几个东西是基于约定的。它更喜欢约定而不是配置。按照惯例,这两件事应该有联系。DbContext的类名匹配连接字符串,以便按照约定正确工作。
https://stackoverflow.com/questions/22122867
复制相似问题