2012-03-06 3 views
19
public class Context : DbContext 
{ 
    public Context(string connString) : base(connString) 
    { 
     Database.SetInitializer(new MyContextInitializer()); 
    } 
//... 

컨텍스트 생성자에 연결 문자열을 전달해야합니다. 문자열은 SQL Compact와 같이 어떻게 표시되어야합니까? 나는이 문자열하려고하면Entity Framework 연결 문자열이 구성에서 없음

은 :이 시도하지,하지만 성공 :

Context db = new Context("Provider=System.Data.SqlServerCe.4.0;Data Source=D:\\Context.sdf"); 

편집 "Data Source=D:\\Context.sdf"

System.Data.ProviderIncompatibleException가 처리되지 않은되었다

메시지는 오류 = 데이터베이스에서 공급자 정보를 가져 오는 동안 발생했습니다.
잘못된 연결 문자열을 사용하여 Entity Framework에서 발생할 수 있습니다. 자세한 내용은 내부 예외를 검사하고 연결 문자열이 올바른지 확인하십시오.

출처 = EntityFramework

그리고이 같은 공급자 언급하려고하면 : "Data Source=D:\\Context.sdf;provider=System.Data.SqlServerCe.4.0"

System.ArgumentException이 = 키워드는 지원되지 않습니다 처리되지 않은

메시지였다 '제공'을.

출처 = System.Data

+0

http://msdn.microsoft.com/en-us/library/bb738533.aspx –

+0

@KrisIvanov, 그다지 효과가 없습니다. – Wonder

+0

"성공하지 못했습니다"보다 자세한 내용을 제공하는 데 도움이됩니다. –

답변

5

난 당신이 항상 EntityConnectionStringBuilder (System.Data.EntityClient)를 사용하는 것이 좋습니다 :

EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder(); 
ecsb.Provider = "System.Data.SqlServerCe.4.0"; 
ecsb.Metadata = "..."; // your metadata 
ecsb.ProviderConnectionString = "Data Source=D:\\Context.sdf"; 

는 당신은 간단한 방법으로 연결 문자열을 생성 할 수 있습니다 :

Context db = new Context(ecsb.ToString()); 

UPDATE :

,

대신 연결, 다음,의 EntityConnection을 만들고이 컨텍스트에 전달하십시오 :

어쨌든
EntityConnection conn = new EntityConnection(ecsb.ToString()); 
Context db = new Context(conn); 

, 메타 데이터가 어디에 있습니까? EntityConnection에서 필요합니다! (정확히 같은 오류) 내 경우

+0

이 문자열을 생성합니다 :'Provider = System.Data.SqlServerCe.4.0; 공급자 연결 문자열 = "데이터 원본 = D : \ Context.sdf"'그리고 나는'예외 인수 : 키워드를 지원하지 않습니다 : 'provider'.' – Wonder

+0

내 대답을 업데이 트했습니다 – tanathos

+0

흠, 내가 어떤 코드를 전달하기 때문에 어떤 – Wonder

3

는 기본 연결 팩토리 변경하여 해결되었다 :

Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0"); 

이 필요가 없기 때문에 연결 문자열에서 공급자에 대한 참조를 남겨주세요 (이런 식으로) . 나는 기본적으로 Database.DefaultConnectionFactory가 SqlConnectionFactory()로 설정되어 있다고 읽었지만 그것에 대한 참조를 찾을 수 없다고 생각한다.

8

MVC 4 코드와 비슷한 오류가 발생했습니다 (업데이트 데이터베이스 실행 중). 내가받은 오류 :

데이터베이스에서 제공 업체 정보를 가져 오는 중에 오류가 발생했습니다.잘못된 연결 문자열을 사용하여 Entity Framework 인해 발생할 수 있습니다. 자세한 내용은 내부 예외를 확인하고 이 연결 문자열이 올바른지 확인하십시오.

local.DB에서 모두 작동하도록 web.config에 중요한 정보가 누락되었습니다. 여기에서 중요한 부분이다 (나는 http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-configuration-file-settings.aspx에서 참고 자료 사용) :

<configuration> 
    <configSections> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 
    </configSections> 
    <connectionStrings> 
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ESTdb-01;Integrated Security=true" providerName="System.Data.SqlClient" /> 
    </connectionStrings> 

    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True" /> 
     </parameters> 
    </defaultConnectionFactory> 
    </entityFramework> 
</configuration> 

을 그리고 좋은 측정을 위해, 여기 내 전체의 Web.config (내가 MVC 4 EF 4.3.1, EF-마이그레이션을 사용하고 있습니다)입니다 :

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 
    </configSections> 
    <connectionStrings> 
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ESTdb-01;Integrated Security=true" providerName="System.Data.SqlClient" /> 
    </connectionStrings> 
    <appSettings> 
    <add key="webpages:Version" value="2.0.0.0" /> 
    <add key="webpages:Enabled" value="true" /> 
    <add key="PreserveLoginUrl" value="true" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    <authentication mode="Forms"> 
     <forms loginUrl="~/Account/Login" timeout="2880" /> 
    </authentication> 
    <pages> 
     <namespaces> 
     <add namespace="System.Web.Helpers" /> 
     <add namespace="System.Web.Mvc" /> 
     <add namespace="System.Web.Mvc.Ajax" /> 
     <add namespace="System.Web.Mvc.Html" /> 
     <add namespace="System.Web.Routing" /> 
     <add namespace="System.Web.WebPages" /> 
     </namespaces> 
    </pages> 
    <profile defaultProvider="DefaultProfileProvider"> 
     <providers> 
     <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" /> 
     </providers> 
    </profile> 
    <membership defaultProvider="DefaultMembershipProvider"> 
     <providers> 
     <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" /> 
     </providers> 
    </membership> 
    <roleManager defaultProvider="DefaultRoleProvider"> 
     <providers> 
     <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" /> 
     </providers> 
    </roleManager> 
    <sessionState mode="InProc" customProvider="DefaultSessionProvider"> 
     <providers> 
     <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" /> 
     </providers> 
    </sessionState> 
    </system.web> 
    <system.webServer> 
    <validation validateIntegratedModeConfiguration="false" /> 
    <modules runAllManagedModulesForAllRequests="true" /> 
    </system.webServer> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" /> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True" /> 
     </parameters> 
    </defaultConnectionFactory> 
    </entityFramework> 
</configuration> 
+2

정확히 어떤 부분이 중요합니까? 연결 문자열 * 및 *을 기본 연결 팩토리로 사용하는 이유는 무엇입니까? – user2246674

+0

감사합니다. 그것은 나에게 아직 효과가 없지만 솔루션을 간단하게 만드는 노력은 좋았습니다. – obesechicken13

0

은 저도 같은 문제를 겪고 그것은 해결되었습니다

1 원하는 Web.config의 포함 된 프로젝트로 관리자 콘솔의 기본 프로젝트 설정

2 - 동일한 프로젝트에 대한 솔루션의 시작 프로젝트를 설정하여 업데이트 명령이 연결 문자열을 찾도록하십시오.

관련 문제