【发布时间】:2018-03-02 23:34:04
【问题描述】:
我使用代码优先创建了一个模型,并且没有在 App.config 中存储密码,而是将其传递给 DbContext 的构造函数:
public TasksWithoutPasswordContext(string nameOrConnectionString) : base(nameOrConnectionString)
{
this.Database.Connection.ConnectionString = nameOrConnectionString;
}
但我在尝试获取我的 DbContext 的 DbSet 时遇到了运行时错误:
using (TasksWithoutPasswordContext contextWithoutPassword = new TasksWithoutPasswordContext(connectionString))
{
foreach (var user in contextWithoutPassword.users)
{
MessageBox.Show(user.user);
}
}
其他信息:在建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:命名管道提供者,错误:40 - 无法打开与 SQL Server 的连接)。
我试图将我的班级标记为下一个:
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class TasksWithoutPasswordContext : DbContext
它没有帮助。
【问题讨论】:
-
你能告诉我们你的连接字符串吗?用 **** 标记您的密码
-
你做了什么错误要求你?你检查过这是否是正确的连接字符串并且你的服务器正在运行吗?
-
@Matthias Burger, server=localhost;user id=test_user1;password=****;database=tasks_for_consultants
-
你用的是mysql吗?也许尝试使用另一个连接字符串:
Server=localhost;Database=tasks_for_consultants;Uid=test_user1;Pwd=**** -
@MatthiasBurger,是的。
标签: c# mysql sql-server entity-framework dbcontext