web.config가 아닌 사용자 지정 구성 파일에서 Entity Framework 4 연결 문자열을 검색하려면 어떻게해야합니까?대체 위치에서 Entity Framework 연결 문자열 가져 오기?
편집 : 기본 생성자 생성 코드를 삭제하고 부분 클래스에서 다시 생성하여 연결 문자열을 사용하는 것이 합리적입니까?
나는 연결 문자열을 포함하여 오버로드 된 메서드를 사용하여 EF 컨텍스트에 대한 모든 참조를 변경하지 않는 것이 좋습니다.
@BrokenGlass : 이것은 우리가 결국 무엇을 :
당신이 연결 문자열을 전달할 수 있습니다DataContext
에 대한 생성자 과부하있다
public partial class STARSEntities
{
private const string _connectionStringFormat = @"metadata=res://*/STARS.EntityModel.STARSModel.csdl|res://*/STARS.EntityModel.STARSModel.ssdl|res://*/STARS.EntityModel.STARSModel.msl;provider=System.Data.SqlClient;provider connection string='Data Source={0};MultipleActiveResultSets=True'";
/// <summary>
/// Initializes a new STARSEntities object using the connection string found in the STARS.xml configuration file.
/// </summary>
/// <remarks>
/// If the STARSEntities class is regenerated from the database, the default constructor needs to be removed from the generated file.
/// </remarks>
public STARSEntities() : base(GetConnectionString(), "STARSEntities")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
private static string GetConnectionString()
{
return string.Format(_connectionStringFormat, ApplicationConfiguration.GetConnectionString("STARS"));
}
}
무엇 EF를 컨텍스트 생성자를 사용하고 있습니까? 구성을 어떻게 든 전달해야합니다. 피할 수는 없습니다. – BrokenGlass
지금은 web.config에서 연결 문자열을 참조하는 기본 생성자를 사용하고 있습니다. – Noel