实体框架连接字符串不是来自配置

本文关键字:配置 框架 连接 字符串 实体 | 更新日期: 2023-09-27 18:26:45

public class Context : DbContext
{
    public Context(string connString) : base(connString) 
    {
        Database.SetInitializer(new MyContextInitializer());
    }
//...

需要将连接字符串传递给上下文构造函数。例如,对于 SQL Compact,字符串应该是什么样子的?试过这个,但没有成功:

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

编辑:

如果我尝试这个字符串:"Data Source=D:''Context.sdf"

System.Data.ProviderIncompatibleException is unhandle

消息=从数据库获取提供程序信息时出错。
这可能是由实体框架使用不正确的连接字符串引起的。 检查内部异常以了解详细信息,并确保连接字符串正确。

源=实体框架

如果我尝试像这样提及提供商:"Data Source=D:''Context.sdf;provider=System.Data.SqlServerCe.4.0"

System.ArgumentException 未处理

消息=不支持关键字:"提供程序"。

源=系统.数据

实体框架连接字符串不是来自配置

我首先在运行更新数据库时遇到了类似的MVC 4代码错误。我得到的错误:

从 数据库。这可能是由于实体框架使用不正确的 连接字符串。检查内部异常以了解详细信息并确保 连接字符串正确。

事实证明,我的web.config中缺少一些关键信息,无法使其与localDB一起使用。以下是重要的部分(我使用了 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-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>

我建议你始终使用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());

更新:

尝试创建实体连接,然后将其传递给上下文,而不是连接:

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

无论如何,元数据在哪里?这是实体连接所必需的!

在我的情况下(完全相同的错误(,它是通过更改默认连接工厂来解决的:

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

在连接字符串中省略对提供程序的任何引用,因为它不是必需的(这样(。我想我读到默认情况下Database.DefaultConnectionFactory设置为SqlConnectionFactory((,但我找不到参考。

我遇到了同样的问题,它已通过以下方式解决:

1-在管理器控制台中将默认项目设置为包含所需web.config的项目

2-将解决方案的启动项目设置为同一项目,以便更新命令查找连接字符串。