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

Należy przekazać ciąg połączenia do konstruktora kontekstu. Jak powinna wyglądać struna, na przykład dla SQL Compact? Próbowałem, ale bez powodzenia:Łańcuch połączenia Entity Framework, a nie konfiguracja

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

Edit:

Gdy próbuję ten ciąg: "Data Source=D:\\Context.sdf"

System.Data.ProviderIncompatibleException był nieobsługiwany

Message = Wystąpił błąd wystąpił podczas pobierania informacji o dostawcy z bazy danych.
Może to być spowodowane przez Entity Framework za pomocą niepoprawnego ciągu połączenia. Sprawdź wewnętrzne wyjątki i sprawdź, czy ciąg połączenia jest poprawny.

Source = EntityFramework

A jeśli próbuję wspomnieć dostawcę takiego: "Data Source=D:\\Context.sdf;provider=System.Data.SqlServerCe.4.0"

System.ArgumentException był nieobsługiwany

Message = Hasło nie są obsługiwane: 'dostawca'.

Source = System.Data

+0

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

+0

@KrisIvanov, to nie działa. – Wonder

+0

Pomoże to zapewnić więcej szczegółów niż "brak sukcesu". –

Odpowiedz

5

Proponuję używać zawsze 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"; 

następnie można wygenerować ciąg połączenia w prosty sposób:

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

UPDATE:

Spróbuj stworzyć EntityConnection, a następnie przekazać go do kontekstu, zamiast połączenia:

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

W każdym razie, gdzie jest metadata? Jest to wymagane przez EntityConnection!

+0

Generuje ten ciąg: 'provider = System.Data.SqlServerCe.4.0; ciąg połączenia dostawcy =" Źródło danych = D: \ Context.sdf "' i otrzymuję 'Argument Exception: Słowo kluczowe nie jest obsługiwane: 'provider'.' – Wonder

+0

Zaktualizowałem moją odpowiedź: – tanathos

+0

Hmm, nie mam żadnych metadanych do przekazania – Wonder

3

W moim przypadku (dokładnie taki sam błąd) został rozwiązany przez zmianę fabrycznie domyślne połączenia:

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

Leave się wszelkie odniesienia do dostawcy w ciągu połączenia, ponieważ nie jest to konieczne (w ten sposób) . Myślę, że czytałem domyślnie Database.DefaultConnectionFactory jest ustawiony na SqlConnectionFactory(), ale nie mogę znaleźć odniesienia dla tego.

8

Najpierw miałem podobny błąd z kodem MVC 4 (podczas uruchamiania bazy danych aktualizacji). Wystąpił błąd:

Wystąpił błąd podczas pobierania informacji o usługodawcy z bazy danych .Może to być spowodowane przez Entity Framework przy użyciu niepoprawnego ciągu połączenia . Sprawdź wewnętrzne wyjątki, aby uzyskać szczegółowe informacje i upewnij się, że ciąg połączenia jest poprawny.

Okazuje się, że brakowało mi kilku ważnych informacji w moim pliku web.config, aby uzyskać wszystko działające z localDB. Oto ważne sekcje (użyłem materiał odniesienia z 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> 

A na dokładkę, oto całe moje web.config (używam MVC 4, EF 4.3.1 EF-migracja):

<?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

Więc jakie dokładnie części są ważne? Po co używać ciągu połączenia * i * domyślnej fabryki połączeń? – user2246674

+0

Dzięki. Nie działa to dla mnie, ale starałem się, aby rozwiązanie było proste. – obesechicken13

0

miałem ten sam problem i został on rozwiązany przez:

1- ustawienie domyślne projekt w konsoli do menedżera projektu, który zawiera pożądany web.config

2- ustawienie projektu startowego rozwiązania dla tego samego projektu, aby polecenie update mogło znaleźć ciąg połączenia.

Powiązane problemy