2014-10-07 2 views
0

WPF 데이터베이스 변환 응용 프로그램에서 Entity Framework를 사용하고 있습니다. 나는 애플 리케이션을 공개에 가깝지만 한가지는 항상 나를 괴롭혔다. DB 컨텍스트는 내가 처음 시도 할 때 항상 실패한다.WPF app : Entity Framework 연결이 처음 실패합니다.

System.Data.SqlClient.SqlException (0x80131904): 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)

내가 그것을 다른 일을하고 있는데 (그리고 이후의 모든 시도에 잘 연결) 때문에 인스턴스에 액세스 할 알고

여기에 내가 오류입니다. 그래서 내 구성에 뭔가 있어야 해.

앱에 대한 기본 app.config가없는 경우이 앱에 대한 기본 app.config가 있습니다. 앱을 사용하여 사용자는 데이터베이스 인스턴스에 연결하고 ConfigurationManager 클래스를 사용하여 설정을 구성 파일에 저장합니다. 연결 문자열을 저장하기 전까지 컨텍스트 호출을하지 않습니다.

샘플의 app.config :

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <appSettings> 
    <add key="ImportUser" value="Admin" /> 
    </appSettings> 
    <connectionStrings> 
    <add name="RockContext" connectionString="Data Source=localhost; 
      Initial Catalog=RockChMS;Integrated Security=True; 
      MultipleActiveResultSets=True;Connection Timeout=15" 
      providerName="System.Data.SqlClient" /> 
    </connectionStrings> 
</configuration> 

EF가 연결됩니다

  • 내가 전에 수동의 app.config을 변경하는 경우 올바른의 app.config 설정
  • 를 사용하여 응용 프로그램을 다시 시작하면 이 프로그램은 EF을 강제 할 수있는 방법이 있나요

을 시작합니다 프로그램을 시작한 후 올바른 연결 문자열을 다시로드하거나 새로 고침 하시겠습니까? 수정 된 연결 문자열을 저장 한 후에야 컨텍스트를 호출 할 것입니다.

답변

1

강제로 EF를 다시로드하는 방법은 없습니다. 그러나 올바른 연결 문자열 이 있으면 연결 문자열을 매개 변수로 사용하는 오버로드 된 ObjectContext 생성자 버전을 사용할 수 있습니다. BuroDBEntities 예를 들어, - 내 개체 컨텍스트 클래스 (공용 부분 클래스 BuroDBEntities : ObjectContext는 {...}) :

private BuroDBEntities Context = new BuroDBEntities(); //will use connection string from app configuration file 

private BuroDBEntities Context = new BuroDBEntities(MyConnectionString); //will use connection string from parameter 
+0

감사를 제안에 대해, 내가 나중에 오늘 밤을 시도 할 것이다! – David

0

내 DB 객체 클래스에 생성자를 추가 한 후, 나는 깨달았다 내가 확인하지 않은 것을 설정을 새로 고칠 때 오른쪽 섹션. ConfigurationManager.RefreshSection 메서드는 대/소문자를 구분하며 일치하지 않으면 오류를 throw하지 않습니다.

지금은 설정을 저장하고 DBContext에 액세스하기 전에 새로 고침 할 수 있습니다

appConfig.Save(ConfigurationSaveMode.Modified); 
ConfigurationManager.RefreshSection("connectionStrings"); 
관련 문제