如何使用实体框架 (v6) 库

本文关键字:v6 框架 何使用 实体 | 更新日期: 2023-09-27 17:55:28

我可以创建一个实体框架控制台应用程序,用于显示罗斯文数据库中的客户。

但是,当我尝试在单独的类库中创建罗斯文实体框架时,该类库由单独的子项目中的简单 C# 控制台应用程序使用,我遇到了一些问题。

我读了 http://www.dotnetcurry.com/showarticle.aspx?ID=617并观看了 http://msdn.microsoft.com/en-us/data/ff628208.aspx 并按照说明将 app.config 中的连接字符串从库复制到控制台模式程序目录,并添加了对我的 Northwind EF 类库的引用。

尝试在我的控制台程序中创建 dbcontext 变量时,我仍然收到此异常:

An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll
Additional information: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

经过更多的谷歌搜索,我尝试添加以下代码:

public abstract class BaseDomainContext : System.Data.Entity.DbContext{
    static BaseDomainContext() {
        var ensureDLLIsCopied = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
    }
}

现在的问题是我在"SqlServer"下面得到了红色的波浪线 - 显然在我的System.Data.Entry副本中没有这样的命名空间。我刚刚使用 nuget 升级到 EF 6。谁能帮我解决这个问题?
谢谢
西格弗里德

周日 3月 30 2014:忘了提:我在Windows 8.1上使用Visual Studio 2013。这是我的控制台模式演示程序中的 app.config,其中大部分是从类库 app.config 复制的。现在我想起来了,谁在寻找那个连接字符串?是类库还是我的控制台模式演示程序?连接字符串的名称应该是什么?

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <connectionStrings>
    <add name="NorthwindEntitiesLib" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=KM;initial catalog=Northwind;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

2014年3月31日星期一:VS 2012 一切正常。我只需添加对类库的引用,将大部分 App.config 文件从库 app.config 剪切并粘贴到控制台模式 app.config,一切正常。我们可以从中得出什么结论?看起来 EF 6 的 VS2013 中存在错误。任何人都可以帮我解决此错误吗?

Ben:我尝试了您的建议,从您发布的 XML 来看,您似乎正在使用 EF 5。我刚刚升级到6。我在VS 2013中仍然遇到相同的错误。

如何使用实体框架 (v6) 库

它在 System.Data 中.dll即 System.Data.SqlClient .

确保您对此有引用。

可以发布连接字符串吗?

这个对我有用 - 我刚刚创建了一个新项目并向该项目添加了一个新的 .edmx 文件并调用:

using (YourDbContext db = new YourDbContext(){///}

这将调用上下文的无参数构造函数,该构造函数使用 app.config 中的默认连接字符串设置。 自己查看构造函数,了解自动生成的内容,并了解正在使用的连接字符串。

<configuration>
    <configSections>
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </configSections>
    <connectionStrings>
        <add name="NorthwindEntitiesLib" connectionString="metadata=res://*/App_Code.Model.csdl|res://*/App_Code.Model.ssdl|res://*/App_Code.Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=KM;initial catalog=Northwind;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    </connectionStrings>
     <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
     </entityFramework>
</configuration>