c# - 如何在类库项目中配置 Entity Framework ?

标签 c# asp.net-mvc entity-framework wcf service

我创建了一个类库项目,其中包含 Entity Framework 对象,该对象将负责我的多个项目的公共(public)数据访问层。将生成的 dll 文件添加到我的域项目并使用类库项目中的实体对象后,我面临以下问题。

在应用程序配置文件中找不到名为“ABC”的连接字符串。

我已经将 edmx 的 Metadata Artifact 处理属性设置为Embed in Output Assembly

App.Config 标记

  <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <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="ABC" connectionString="metadata=res://*/Entity.ABC.csdl|res://*/Entity.ABC.ssdl|res://*/Entity.ABC.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\ABC.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
</configuration>

实体代码如下

public ABC_IntegrationEntities()
            : base("name=ABC")
        {
        }

还有一件事:我正在从数据目录“App_Data”访问数据库。

我引用的博客来自 dotnetcurry.com .

最佳答案

基于 the link你给了,可能你错过了这部分。

Now you can either create your own .config file in this project and add the connectionstring entry of your model or simply copy the App.config file that you created in the MyEntityModel project, in your Console App project. We will adopt the simpler way. Right click the project > Add Existing Item > Go to the MyEntityModel project and add the App.config file.

解释:

当您创建一个库并且该库被另一个项目引用时,EF 将读取与启动项目相关的配置。您可以执行以下操作之一。

  • 从库的配置中复制配置(复制文件并粘贴以启动项目)
  • 创建一个新配置,但复制连接字符串信息和其他 EF 相关部分
  • 通过添加现有项目添加现有配置并将其添加为库配置的链接

关于c# - 如何在类库项目中配置 Entity Framework ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25405873/

相关文章:

javascript - jQuery UI ui-autocomplete-loading 微调器在找到结果时不停止/消失

c# - .Net Core 2.0 中 Controller 和 BaseController 中的依赖注入(inject)重复

c# - 使用 Entity Framework 将 bool 属性映射到 Oracle

c# - xsd.exe 属性顺序

c# - 这是最流畅的 nhibernate 抓取策略吗?

.net - 从 Asp.Net Web Api 项目中删除引用

c# - Entity Framework 6.1.3 : Projection - load children of children directly

entity-framework - Entity Framework外键代码第一次迁移错误

c# - 一条SQL汇总不同的 'group by'级别

c# - 是否可以测试给定表达式是否可以转换为 SQL *而无需*连接到 SQL 数据库?