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

Cần chuyển chuỗi kết nối tới hàm tạo ngữ cảnh. Chuỗi nên trông như thế nào, ví dụ, cho SQL Compact? Cố gắng này, nhưng không thành công:Chuỗi kết nối khung thực thể không phải từ cấu hình

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

Edit:

Nếu tôi cố gắng chuỗi này: "Data Source=D:\\Context.sdf"

System.Data.ProviderIncompatibleException là unhandled

nhắn = Lỗi xảy ra trong khi nhận thông tin nhà cung cấp từ cơ sở dữ liệu.
Điều này có thể được gây ra bởi khung thực thể bằng cách sử dụng một chuỗi kết nối không chính xác. Kiểm tra các ngoại lệ bên trong để biết chi tiết và đảm bảo rằng chuỗi kết nối là chính xác.

Source = EntityFramework

Và nếu tôi cố gắng đề cập đến các nhà cung cấp như thế này: "Data Source=D:\\Context.sdf;provider=System.Data.SqlServerCe.4.0"

System.ArgumentException là unhandled

nhắn = từ khóa không được hỗ trợ: 'cung cấp'.

Source = System.Data

+0

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

+0

@KrisIvanov, không hoạt động. – Wonder

+0

Nó sẽ giúp cung cấp thêm chi tiết hơn "không thành công". –

Trả lời

5

tôi đề nghị bạn sử dụng luôn là 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"; 

sau đó bạn có thể tạo ra chuỗi kết nối một cách đơn giản:

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

UPDATE:

Hãy cố gắng tạo EntityConnection, sau đó vượt qua nó vào bối cảnh, thay vì kết nối:

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

Dù sao, mà là siêu dữ liệu? Nó được yêu cầu bởi EntityConnection!

+0

Nó tạo ra chuỗi này: 'provider = System.Data.SqlServerCe.4.0; chuỗi kết nối nhà cung cấp =" Nguồn dữ liệu = D: \ Context.sdf "' và tôi nhận được 'Ngoại lệ đối số: Từ khóa không được hỗ trợ: 'provider'.' – Wonder

+0

Tôi đã cập nhật câu trả lời của mình – tanathos

+0

Hmm, tôi không có bất kỳ siêu dữ liệu nào để vượt qua – Wonder

3

Trong trường hợp của tôi (chính xác lỗi tương tự), nó đã được giải quyết bằng cách thay đổi các nhà máy kết nối mặc định:

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

Bỏ bất kỳ tài liệu tham khảo để cung cấp dịch vụ trong chuỗi kết nối của bạn bởi vì nó không phải là cần thiết (bằng cách này) . Tôi nghĩ rằng tôi đọc rằng theo mặc định các Database.DefaultConnectionFactory được thiết lập để SqlConnectionFactory() nhưng tôi không thể tìm thấy một tài liệu tham khảo cho điều đó.

8

Tôi đã gặp lỗi tương tự với mã MVC 4 trước (trong khi đang chạy cơ sở dữ liệu cập nhật). Lỗi tôi nhận được:

Đã xảy ra lỗi khi nhận thông tin nhà cung cấp từ cơ sở dữ liệu .Điều này có thể do Khung thực thể gây ra sử dụng chuỗi kết nối không chính xác . Kiểm tra các ngoại lệ bên trong để biết chi tiết và đảm bảo rằng chuỗi kết nối là chính xác.

Hóa ra tôi đã thiếu một số thông tin quan trọng trong web.config để làm cho nó hoạt động với localDB. Dưới đây là những phần quan trọng (tôi đã sử dụng tài liệu tham khảo từ 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> 

Và đối với biện pháp tốt, đây là toàn bộ web.config của tôi (Tôi đang sử dụng MVC 4, EF 4.3.1, EF-Migrations):

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

Vì vậy, chính xác phần nào là quan trọng? Tại sao lại sử dụng chuỗi kết nối * và * nhà máy kết nối mặc định? – user2246674

+0

Cảm ơn. Nó không làm việc cho tôi nhưng nỗ lực để làm cho giải pháp đơn giản là tốt đẹp. – obesechicken13

0

tôi đã cùng một vấn đề và nó đã được giải quyết bằng cách:

1- thiết lập các dự án mặc định trong quản lý giao diện điều khiển để dự án có chứa web.config mong muốn

2- thiết lập dự án khởi động cho giải pháp cho cùng một dự án để lệnh cập nhật tìm chuỗi kết nối.

Các vấn đề liên quan