2017-09-21 21 views
1

私は、コード最初使用してモデルを作成し、App.configファイルにパスワードを格納することなく接続することはできません、私の代わりにDbContextのコンストラクタに渡します私のDbContextのDbSet取得する:Entity FrameworkのMySQLのDbContextは

using (TasksWithoutPasswordContext contextWithoutPassword = new TasksWithoutPasswordContext(connectionString)) 
{ 
    foreach (var user in contextWithoutPassword.users) 
    { 
     MessageBox.Show(user.user); 
    } 
} 

Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server).

を私は次のように私のクラスをマークしてみました:

[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))] 
public class TasksWithoutPasswordContext : DbContext 

それは役に立たなかった。

+0

接続文字列を表示できますか?あなたのパスワードに**** –

+0

と記入してください。エラーが表示されましたか?これが正しい接続文字列であり、サーバーが動作しているかどうかを確認しましたか? –

+0

@Matthias Burger、server = localhost;ユーザID = test_user1;パスワード= ****;データベース= tasks_for_consultants –

答えて

0

あなたは既に解決策を自分で見つけたように、私は答えを追加:デフォルト・ベースのコンストラクタ(パラメータのないもの)とのセットを使用することができ、あなたはDbContextから継承しているとき

を、ベースのコンストラクタを呼び出しますコンストラクタ内の接続文字列:

public TasksWithoutPasswordContext(string nameOrConnectionString) : base() 
{ 
    this.Database.Connection.ConnectionString = nameOrConnectionString; 
} 
関連する問題